Sherlock and the Valid String

  • + 0 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";
    }