Weighted Uniform Strings

  • + 0 comments

    My Java solution:

    public static List<String> weightedUniformStrings(String s, List<Integer> queries) {
            //process the weight for each uniform substring and store it in set U
            Set<Integer> U = new HashSet<>();
            //store the total weight to account for substrings greater than 1
            int totalWeight = 0;
            for(int i = 0; i < s.length(); i++){
                //get the current weight
                int currentWeight = s.charAt(i) - 96;
                //if the weight before it is the same, add the total weight to the set
                if(i > 0 && s.charAt(i - 1) == s.charAt(i)){
                    totalWeight += currentWeight;
                    U.add(totalWeight);
                }
                else{
                    totalWeight = currentWeight;
                    U.add(currentWeight);
                }
            }
            //check if each query is in the set
            List<String> queryAccuracies = new ArrayList<>();
            for(int query: queries){
                if(U.contains(query)) queryAccuracies.add("Yes");
                else queryAccuracies.add("No");
            }
            //return list of queriy accuracies
            return queryAccuracies;
        }