You are viewing a single comment's thread. Return to all comments →
Here is the solution in java passed for all test cases
static String happyLadybugs(String b) { boolean flag=false; if(b.length()==1&&b.charAt(0)!='_'){ return "NO"; } int count=0; for(int i=0;i<b.length();i++){ if(b.charAt(i)=='_'){ flag=true; count++; } } if(count==b.length()){ return "YES"; } if(flag==false){ for(int i=1;i<b.length();i++){ if(i==b.length()-1){ if(b.charAt(i)!=b.charAt(i-1)){ return "NO"; } return "YES"; } if(b.charAt(i)!=b.charAt(i-1)&&b.charAt(i)!=b.charAt(i+1)){ return "NO"; } } return "YES"; } else{ char[] arr=b.toCharArray(); Arrays.sort(arr); if(arr[0]!=arr[1]){ return "NO"; } for(int i=1;i<arr.length;i++){ if(arr[i]=='_'){ if(arr[i-1]!=arr[i-2]){ return "NO"; } return "YES"; } if(arr[i]!=arr[i-1]&&arr[i]!=arr[i+1]){ return "NO"; } } } return "YES"; }
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 →
Here is the solution in java passed for all test cases