Weighted Uniform Strings

  • + 0 comments

    Java Solution

        public static List<String> weightedUniformStrings(String s, List<Integer> queries) {
            Set<Integer> weights = new HashSet<>();
    
            for(int i = 0; i < s.length();) {
                char c = s.charAt(i);
                int weight = c - 'a' + 1;
                weights.add(weight);
                
                int j = i+1;
                int newWeight = weight;
                while(j < s.length() && s.charAt(j) == s.charAt(i)){
                    newWeight += weight;
                    weights.add(newWeight);
                    j++;
                }
                
                i = j;
            }
            
            List<String> results = new ArrayList<>();
            for (int i = 0; i < queries.size(); i++){
                int value = queries.get(i);
                if (weights.contains(value)) {
                    results.add("Yes");
                } else {
                    results.add("No");
                }
            }
            return results;
        }