You are viewing a single comment's thread. Return to all comments →
Java:
static int getWeight(char c){ return (int) (c - 96); } public static List<String> weightedUniformStrings(String s, List<Integer> queries) { List<String> result = new ArrayList<>(); List<Integer> weights = new ArrayList<>(); HashMap<Character, Integer> map = new HashMap<>(); int current_len = 1; int n = s.length(); for(int i = 0; i < n; i++){ char c = s.charAt(i); if(!map.containsKey(c)) map.put(c, 1); else{ if( c == s.charAt(i - 1) ){ current_len ++; if( i == n-1 || c != s.charAt(i + 1) ){ map.put(c, Math.max(current_len, map.get(c)) ); current_len = 1; } } } } for(char key:map.keySet()){ int value = map.get(key); int weight = getWeight(key); for(int i = 1; i <= value; i++){ weights.add( i * weight ); } } for(int query:queries){ if(weights.contains(query)) result.add("Yes"); else result.add("No"); } return result; }
Seems like cookies are disabled on this browser, please enable them to open this website
Weighted Uniform Strings
You are viewing a single comment's thread. Return to all comments →
Java: