You are viewing a single comment's thread. Return to all comments →
public static String isValid(String s) { int []rows = new int[26]; for(int i=0; i<s.length();i++) { rows[s.charAt(i) - 'a']++; } HashMap<Integer, Integer> bank = new HashMap<>(); for(int i=0; i<rows.length;i++) { if(rows[i] > 0) { bank.put(rows[i], bank.getOrDefault(rows[i], 0)+1); } } System.out.print(bank); if(bank.size() == 1) return "YES"; else if(bank.size() == 2) { List<Integer> store = new ArrayList(bank.keySet()); Collections.sort(store); if(store.get(0) == 1 && bank.get(1) == 1) return "YES"; if(store.get(0) == store.get(1)-1 && (bank.get(store.get(0)) == 1 || bank.get(store.get(1)) == 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 →