We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
/* All the ladybugs are possible to be made happy if there is at least one empty * cell (underscore) and there is no color with only one ladybug. First we check * if there is any color with only one ladybug. If so we print "NO". Then we * check if there is at least one empty cell. If not, the ladybugs must already * be happy without moving. If there isn't any empty cell to move the ladybugs * around we check if all the initial positions are happy. If not we print "NO". * If it passes the tests we print "YES". Either the ladybugs are happy or are * possible to be made. In order to determine if there is any color with only * one ladybug we need a frequency table to hold how many times each color * occurs in the string. You can use an array with a size of 26 each index * corresponding to one letter A-Z or a hashmap to store the frequencies of each * color. */// WITH ARRAYclassResult{publicstaticStringhappyLadybugs(Stringb){StringhappyString="YES";StringnotHappyString="NO";booleanhasUnderScore=false;int[]freqTable=newint[26];// To store frequency of ladybugs (A-Z). A' frequency is// stored at int[0] and Z's at int[25]// Step 1: Count frequencies of each ladybug and check for underscoresfor(charc:b.toCharArray()){if(c=='_'){hasUnderScore=true;// Flag to track if there's at least one// underscore}else{freqTable[c-'A']++;}}// Step 2: Check if there's any ladybug with only 1 occurrenceif(hasSingleOccurence(freqTable)){returnnotHappyString;}// Step 3: If there are no underscores, check if the arrangement is already// happyif(!hasUnderScore&&!isAlreadyHappy(b)){returnnotHappyString;}// Otherwise happyreturnhappyString;}// Method to check if any ladybug has only a single occurrenceprivatestaticbooleanhasSingleOccurence(int[]freq){for(inti=0;i<26;i++){if(freq[i]==1){returntrue;}}returnfalse;}// Method to check if the string is already in a "happy" state (no// underscores)privatestaticbooleanisAlreadyHappy(Stringb){for(inti=0;i<b.length();i++){charcurrent=b.charAt(i);if((i>0&&b.charAt(i-1)==current)||(i<b.length()-1&&b.charAt(i+1)==current)){continue;}returnfalse;// found an unhappy ladybug}returntrue;}}// WITH HASHMAPclassResult{/* * Complete the 'happyLadybugs' function below. * * The function is expected to return a STRING. * The function accepts STRING b as parameter. */publicstaticStringhappyLadybugs(Stringb){StringhappyString="YES";StringnotHappyString="NO";booleanhasUnderScore=false;Map<Character,Integer>freqTable=newHashMap<>();// Step 1: Count frequencies of each ladybug and check for underscoresfor(charc:b.toCharArray()){if(c=='_'){hasUnderScore=true;// Flag to track if there's at least one underscore}else{freqTable.put(c,freqTable.getOrDefault(c,0)+1);}}// Step 2: Check if there's any ladybug with only 1 occurrenceif(hasSingleOccurence(freqTable)){returnnotHappyString;}// Step 3: If there are no underscores, check if the arrangement is// already happyif(!hasUnderScore&&!isAlreadyHappy(b)){returnnotHappyString;}// Otherwise happyreturnhappyString;}// Method to check if any ladybug has only a single occurrenceprivatestaticbooleanhasSingleOccurence(Map<Character,Integer>freq){for(charkey:freq.keySet()){// If a character is not '_' and occurs only once, it's impossible to// make them happyif(freq.get(key)==1){returntrue;}}returnfalse;}// Method to check if the string is already in a "happy" state (no// underscores)privatestaticbooleanisAlreadyHappy(Stringb){for(inti=0;i<b.length();i++){charcurrent=b.charAt(i);if((i>0&&b.charAt(i-1)==current)||(i<b.length()-1&&b.charAt(i+1)==current)){continue;}returnfalse;// found an unhappy ladybug}returntrue;}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Happy Ladybugs
You are viewing a single comment's thread. Return to all comments →