Sherlock and the Valid String

  • + 0 comments
     public static String isValid(String s) {
            Map<Character,Integer> map = new HashMap<>();
            int n = s.length();
            for(int i=0 ; i<n ; i++){
                map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0)+1);
            } 
            Map<Integer,Integer> freqMap = new HashMap<>();
            for(int fq : map.values())
            {
                freqMap.put(fq, freqMap.getOrDefault(fq, 0)+1);
            }
              if (freqMap.size() == 1) {
                return "YES"; 
            }
              if (freqMap.size() == 2) {
                for(int i : freqMap.values()){
                    if(i==1) return "YES";
                }
            }
    
                can any one explain why it is paasing only 18/20 test cases? also please give the failed test case.
    
                `        
        return "NO";
    
    }