You are viewing a single comment's thread. Return to all comments →
what is wrong with this solution it is failing test cases 3 and 5?
public static String isValid(String s) { // Write your code here HashMap<Character,Integer> map = new HashMap<>(); for(int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if(map.containsKey(ch)) { map.put(ch,map.get(ch) + 1); }else map.put(ch,1); } int maxFreq = 0; int maxEle = 0; int[] arr = new int[s.length() + 1]; for(Map.Entry<Character,Integer> entry : map.entrySet()) { arr[entry.getValue()]++; } for(int i = 0; i < arr.length; i++) { if(arr[i] > maxFreq) { maxFreq = arr[i]; maxEle = i; } } int freq = maxEle; int count = 0; for(Map.Entry<Character,Integer> entry : map.entrySet()) { int curr = entry.getValue(); if(curr != freq) { count++; } } return count ==** 0 || count == 1 ? "YES" : "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 →