Weighted Uniform Strings

  • + 0 comments

    Here is my Java 8 (15) solution, feel to to ask me if you have any questions.

    public static List<String> weightedUniformStrings(String s, List<Integer> queries) {
        Set<Integer> weightSet = new HashSet<>();
        
        //Perform founding weight set of the given string 
        String uniformStringRegex = "([a-z])\\1*";
        Matcher m = Pattern.compile(uniformStringRegex).matcher(s);
        while(m.find()) {
            String uniform = m.group();
            int characterWeight = uniform.charAt(0) - 'a' + 1;
            //find out the weights of the found uniform string
            for(int multiplier = 1; multiplier <= uniform.length(); multiplier++) {
                int weight = multiplier * characterWeight;
                weightSet.add(weight);
            }
        }
        
        //perform querying
        List<String> results = new ArrayList<>();
        for (int query : queries) {
            if(weightSet.contains(query)) {
                results.add("Yes");
            }
            else results.add("No");
        }
        
        return results;
    }