Weighted Uniform Strings

Sort by

recency

|

794 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

    Simple C++ solution:

        vector<string> ans;
        char lastc = s[0]; int lastw = s[0] - 96; int sz = s.size();
        set<int>w; w.insert(lastw);
        for (int i=1;i<sz;++i)  {
            if (s[i] == lastc)  lastw+=s[i]-96;
            else    lastw = s[i] - 96; 
            lastc = s[i];
            w.insert(lastw);
        } for (auto q:queries)  {
            if (w.find(q) == w.end())  ans.push_back("No");
            else ans.push_back("Yes"); }
        return ans;
    
  • + 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;
        }
    
  • + 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]
    
  • + 1 comment

    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

      I had the same problem. I got rid of the sum function and used multiplication. All tests passed.