Weighted Uniform Strings

Sort by

recency

|

790 Discussions

|

  • + 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;
    }
    
  • + 0 comments

    My answer in Typpescript, simple, noted

    function weightedUniformStrings(s: string, queries: number[]): string[] {
        /**
         * idea is calculate the weight of each character and it pairs
         * then compare it with query, any included are 'Yes' orelse 'No'
         * 
         * example:
         * [s]                  'abccddde'
         *                      [a b c cc d dd ddd e]
         * [ws] will be         [1 2 3 6  4 8  12  5]
         * [queries]            [1, 3, 12, 5, 9, 10]
         *                       x  x  x   x  _  _
         * [return]              Y  Y  Y   Y  N  N
         */
    
        let ws = []
        let cl = null
        let cc = 0
        for (let i = 0; i < s.length; i++) {
            let cd = s.charCodeAt(i) - 97 + 1
    
            if (!cl || cl != cd) { cl = cd; cc = 0 }
            ws.push(cl * ++cc)
        }
    
        return queries.map(query => ws.includes(query) ? 'Yes' : 'No')
    }
    
  • + 0 comments
    public static List<String> weightedUniformStrings(String s, List<Integer> queries) {
        // Write your code here
        List<Integer> output = new ArrayList<>();
        List<String> weight = new ArrayList<>();
        int n=0;
        for (int i = 0; i < s.length(); i+=n) {
            n=0;
            for (int j = i; j < s.length(); j++) {
                if (s.charAt(i)==s.charAt(j)) {
                    n++;
                }else{
                    j=s.length();
                }
            }
            for (int j = 1; j < n+1; j++) {
                output.add(j*((int)s.charAt(i)%96));
            }
        }
        for (Integer integer : queries) {
            if (output.contains(integer)) {
                weight.add("Yes");
            }else{
                weight.add("No");
            }
        }
        
        return weight;
        
        
    
        }