Weighted Uniform Strings

  • + 0 comments
        public static List<String> weightedUniformStrings(String s, List<Integer> queries) {
        // Write your code here
        List<String> ans = new ArrayList<>();
        HashSet<Integer> weights = new HashSet<>();
        
        int currentweight = 0;
        char prevChar = ' ';
        
        for(int i = 0; i < s.length(); i++) {
            char currentChar = s.charAt(i);
            int charWeight = currentChar - 'a' + 1;
            
            if(currentChar == prevChar) {
                currentweight += charWeight;
            } else {
                currentweight = charWeight;
            }
            weights.add(currentweight);
            prevChar = currentChar;
        }
        
        for(int i : queries) {
                if(weights.contains(i)) {
                    ans.add("Yes");
                } else {
                ans.add("No");
                }
        }
            return ans;
        }