Sherlock and the Valid String

  • + 0 comments

    I am using this java solution but still getting 2 test case failed anyone hlep me , thanks in advance

    Map<Character,Integer> ma=new HashMap<>();
    
            for(int i=0;i<s.length();i++)
            {
                if(!ma.containsKey(s.charAt(i)))
                {
                    ma.put(s.charAt(i),1);
                }
                else {
                    ma.computeIfPresent(s.charAt(i),(k,v)->++v);
                }
            }
    
             Integer[] op= ma.values().toArray(Integer[]::new);
    
            Arrays.sort(op);
    
            if(op[0]==op[op.length-1])
            {
                return "YES";
            }
      if(op[0]==1 && op[1]==op[op.length-1])
            {
               return "YES";
            }
           
                if(op[0]==op[1] && op[1]==op[op.length-2] && (op[1]==op[op.length-1]-1))
                {
                    return "YES";
                }
            
        
    
    return "NO";
        }