Weighted Uniform Strings

Sort by

recency

|

781 Discussions

|

  • + 0 comments

    Python

    def weightedUniformStrings(s, queries):
        st = set()
        uniform = [max(re.findall(f'{i}+', s)) for i in set(s)]
        [st.add((ord(j[0]) - 96) * n) for j in uniform for n in range(1, len(j)+1)]
        return  ['Yes' if q in st else 'No' for q in queries]
        
    
  • + 0 comments

    Java O(N)

    public static List<String> weightedUniformStrings(String s, List<Integer> queries) {
            HashSet<Integer> set = new HashSet<>();
            HashSet<Character> helper = new HashSet<>();
            List<String> result = new ArrayList<>();
            int weight = 0;
            for (char c : s.toCharArray()) {
                if (!helper.isEmpty() && !helper.contains(c)) {
                    weight = 0;
                    helper.clear();
                }
                helper.add(c);
                weight += c - 'a' + 1;
                set.add(weight);
    
            }
            for (int q : queries) 
                result.add(set.contains(q) ? "Yes" : "No");
            return result;
        }
    
  • + 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

    https://github.com/Achintha444/problem-solving-hackerrank-js/blob/main/85-weighted-uniform-strings.js

    JS answer

  • + 0 comments

    **python ** def weightedUniformStrings(s, queries): """ Determines if each query value is a valid weight of a substring in the given string.

    Args:
        s (str): The input string.
        queries (list): A list of query values.
    
    Returns:
        list: A list of "Yes" or "No" for each query, indicating whether the query value is a valid weight.
    """
    
    weights = set()
    weight = ord(s[0]) - 96
    weights.add(weight)
    
    for i in range(1, len(s)):
        if s[i] == s[i - 1]:
            weight += ord(s[i]) - 96
        else:
            weight = ord(s[i]) - 96
        weights.add(weight)
    
    return ["Yes" if q in weights else "No" for q in queries]
    

    if name == 'main': s = input() queries_count = int(input().strip())

    queries = []
    
    for _ in range(queries_count):
        queries_item = int(input().strip())
        queries.append(queries_item)
    
    result = weightedUniformStrings(s, queries)
    
    print('\n'.join(result))