We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Strings
  4. Weighted Uniform Strings
  5. Discussions

Weighted Uniform Strings

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 694 Discussions, By:

recency

Please Login in order to post a comment

  • dnacher
    5 hours ago+ 0 comments

    why this code is giving me a runTime error? I tried one of the test that is failing and I get the correct answer. I don't know why the submit is getting me an error.

    static List<String> abc = Arrays.asList("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
    
        public static List<String> weightedUniformStrings(String s, List<Integer> queries) {
            // Write your code here
            List<String> intermediate = new ArrayList<>();
            List<String> finalString = new ArrayList<>();
            char a_previousChar = ' ';
            String word = "";
            for (int i=0; i<s.length(); i++){
                char a_actualChar = s.charAt(i);
                if(a_previousChar==a_actualChar){
                    word+= a_actualChar;
                    intermediate.add(word);
                }else {
                    word = String.valueOf(s.charAt(i));
                    intermediate.add(word);
                }
                a_previousChar = s.charAt(i);
            }
            List<Integer> listInteger = new ArrayList<>();
            for(String str: intermediate){
                listInteger.add(getValues(str));
            }
            createfinalList(listInteger, queries, finalString);
            return finalString;
        }
        
        public static void createfinalList(List<Integer> listInteger, List<Integer> queries, List<String> finalString){
            for(Integer i: queries){
                if(listInteger.contains(i)){
                    finalString.add("Yes");
                }else{
                    finalString.add("No");
                }
            }
        }
    
        public static int getValues(String str){
            int total = 0;
            for(int i=0; i<str.length();i++){
                total+= abc.indexOf(String.valueOf(str.charAt(i)))+1;
            }
            return total;
        }
    
    0|
    Permalink
  • ivalex01
    1 week ago+ 0 comments

    simple O(N) solution in Python:

    def weightedUniformStrings(s, queries):
        # Write your code here
        weights = set()
        for i, c in  enumerate(s):
            u = ord(c) - ord('a') + 1
            if i > 0 and c == s[i-1]:
                w += u
            else:
                w = u
            if w not in weights:
                weights.add(w)
        result = []       
        for q in queries:
            if q in weights:
                result.append("Yes")
            else: 
                result.append("No")
        return result
    
    1|
    Permalink
  • alban_tyrex
    3 weeks ago+ 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|
    Permalink
  • sphericalconic
    1 month ago+ 0 comments

    JAVA

    public static List<String> weightedUniformStrings(String s, List<Integer> queries) {
            Set<Integer> weights = new HashSet<>();
            int count = 1;
            List<String> result = new ArrayList<>();
            for (int i = 0; i < s.length(); i++) {
                weights.add(count * ((int) s.charAt(i) - 96));
                if (i + 1 < s.length() && s.charAt(i) == s.charAt(i + 1)) {
                    count++;
                } else {
                    count = 1;
                }
            }
            for (Integer query : queries) {
                if (weights.contains(query)) {
                    result.add("Yes");
                } else {
                    result.add("No");
                }
            }
            return result;
        }
    
    0|
    Permalink
  • lambdadelta_
    2 months ago+ 0 comments

    Python 3

    import string
    import itertools
    
    def weightedUniformStrings(s, queries):
        alpha = string.ascii_lowercase
        weights = dict(zip(alpha, itertools.count(1)))
        groups = (g for k, g in itertools.groupby(s))
        U = set()
        for group in groups:
            substr_weights = (weights[c] for c in group)
            substr_U = itertools.accumulate(substr_weights)
            U.update(substr_U)
                
        yes_no = {True:"Yes", False: "No"}
        return [yes_no[q in U] for q in queries]
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy