You are viewing a single comment's thread. Return to all comments →
public static String isValid(String s) { // Write your code here Map<Character,Integer> map = new HashMap<>(); for(char c : s.toCharArray()){ map.put(c,map.getOrDefault(c,0)+1); } TreeMap<Integer,Integer> count = new TreeMap<>(); for(int i : map.values()){ count.put(i,count.getOrDefault(i,0)+1); } System.out.println(map +" , " + count); if(count.size() == 1){ return "YES"; }else if(count.size() == 2){ int key1 = count.firstKey(); int key2 = count.lastKey(); int value1 = count.get(key1); int value2 = count.get(key2); if((key1 == 1 && value1 == 1) || (key2 - key1 == 1 && value2 == 1)){ return "YES"; } } return "NO"; }
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and the Valid String
You are viewing a single comment's thread. Return to all comments →