You are viewing a single comment's thread. Return to all comments →
public static String happyLadybugs(String str) { if(Arrays.stream(str.split("")).distinct().allMatch(s -> s.equals("_"))) return "YES"; Map<String, Long> map = Arrays. stream(str.split("")). collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); if(map.entrySet().stream().filter(en -> !en.getKey().equals("_")).anyMatch(en -> en.getValue()==1L)) return "NO"; if(str.indexOf("_") == -1) { for(int i=0;i<str.length();i++) { if(i==0) { if(str.charAt(i) != str.charAt(i+1)) return "NO"; } else if(i==str.length()-1) { if(str.charAt(i) != str.charAt(i-1)) return "NO"; } else { if(str.charAt(i) != str.charAt(i+1) && str.charAt(i) != str.charAt(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 →