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.
staticStringhappyLadybugs(Stringb){int[]lb=newint['Z'-'A'+1];booleanesp=false;//The number of ladybirds for each color is saved in lb//and evaluated if there is at least one spacefor(charc:b.toCharArray()){if(c!='_'){lb[c-'A']++;}else{esp=true;}}//If there are no spaces, all the ladybugs should have an equal partnerif(!esp){for(inti=1;i<b.length()-1;i++){if(b.charAt(i)!=b.charAt(i-1)&&b.charAt(i)!=b.charAt(i+1)){return"NO";}}}//If there are spaces, there can not be a ladybug of a single colorfor(inti:lb){if(i==1)return"NO";}//If there is no unhappy ladybugreturn"YES";}
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 →
My Java Solution