Weighted Uniform Strings

  • + 0 comments

    Java O(N)

    public static List<String> weightedUniformStrings(String s, List<Integer> queries) {
            HashSet<Integer> set = new HashSet<>();
            HashSet<Character> helper = new HashSet<>();
            List<String> result = new ArrayList<>();
            int weight = 0;
            for (char c : s.toCharArray()) {
                if (!helper.isEmpty() && !helper.contains(c)) {
                    weight = 0;
                    helper.clear();
                }
                helper.add(c);
                weight += c - 'a' + 1;
                set.add(weight);
    
            }
            for (int q : queries) 
                result.add(set.contains(q) ? "Yes" : "No");
            return result;
        }