Weighted Uniform Strings

Sort by

recency

|

792 Discussions

|

  • + 0 comments

    I managed to find a much faster solution using sets!

    def weightedUniformStrings(s, queries):
        findweight = {"a":1, "b":2, "c":3, "d":4, "e":5, "f":6, "g":7, "h":8, "i":9, "j":10, "k":11, "l":12, "m":13, "n":14, "o":15, "p":16, "q":17, "r":18, "s":19, "t":20, "u":21, "v":22, "w":23, "x":24, "y":25, "z":26}
        uniforms = {findweight[s[0]]}
        count = 1
        for i in range(1, len(s)):
            if s[i] == s[i - 1]:
                count += 1
                uniforms.add(findweight[s[i]] * count)
            else:
                count = 1
                uniforms.add(findweight[s[i]])
        return ["Yes" if query in uniforms else "No" for query in queries]
    
  • + 0 comments

    My Python solution is too slow and doesn't pass a lot of the test cases. Can anyone help me fix my code?

    def weightedUniformStrings(s, queries):
        findweight = {"a":1, "b":2, "c":3, "d":4, "e":5, "f":6, "g":7, "h":8, "i":9, "j":10, "k":11, "l":12, "m":13, "n":14, "o":15, "p":16, "q":17, "r":18, "s":19, "t":20, "u":21, "v":22, "w":23, "x":24, "y":25, "z":26}
        uniforms = [s[0]]
        for i in range(1, len(s)):
            if s[i] == s[i - 1]:
                uniforms.append(uniforms[-1] + s[i])
            else:
                uniforms.append(s[i])
        weights = [sum([findweight[letter] for letter in string]) for string in uniforms]
        return ["Yes" if query in weights else "No" for query in queries]
    
  • + 0 comments

    Simple C++ Solution, video here : https://youtu.be/xhpzPQBB9ts

    vector<string> weightedUniformStrings(string s, vector<int> queries) {
        // build the set
        map<int, int> mp;
        int last = 0;
        for(int i = 0; i < s.size(); i++){
            if(i != 0 && s[i] == s[i-1]){
                last += s[i] - 'a' + 1;
                mp[last] = 1;
            }
            else{
                last = s[i] - 'a' + 1;
                mp[last] = 1;
            }
        }
        vector<string>result;
        for(int i = 0; i < queries.size(); i++){
            if(mp[queries[i]]) result.push_back("Yes");
            else result.push_back("No");
        }
        return result;
        
    }
    
  • + 0 comments
    List<String> retList = new ArrayList<>();
            List<Integer> sumList = new ArrayList<>();
            String regex = "([a-z])\\1*";
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(s);
            while(m.find()){
                String sub = m.group();
                char curr = sub.charAt(0);
                int count = sub.length();
                for(int i=0;i<count;i++){
                    sumList.add(((int)curr-97+1)*(i+1));
                }
                s=s.replace(sub,"");
            }
            
            for(int i=0;i<queries.size();i++){
                if(sumList.contains(queries.get(i))){
                    retList.add("Yes");
                }else{
                    retList.add("No");
                }
            }
            return retList;
    
  • + 0 comments

    Perl:

    sub weightedUniformStrings {
        my ($s, $queries) = @_;
        my %weights;
        my $prev_char = '';
        my $current_weight = 0;
    
        foreach my $char (split //, $s) {
            my $char_weight = ord($char) - ord('a') + 1;
            
            if ($char eq $prev_char) {
                $current_weight += $char_weight;
            } else {
                $current_weight = $char_weight;
            }
    
            $weights{$current_weight} = 1;
            $prev_char = $char;
        }
    
        my @results;
        foreach my $query (@$queries) {
            push @results, exists $weights{$query} ? "Yes" : "No";
        }
    
        return @results;
    }