Weighted Uniform Strings

Sort by

recency

|

785 Discussions

|

  • + 3 comments
    public static List<String> weightedUniformStrings(String s, List<Integer> queries) {
        // Write your code here
    		// Youtube : The Adarsh 1M
        Set<Integer>hs = new HashSet<>();
            int prev = 0;
            char cho = s.charAt(0);
            // Map<Character, Integer> mp = new HashMap<>();
            for(char ch : s.toCharArray()) {
                int val = (ch-'a') + 1;
                if(ch == cho){
                    val = val+prev;
                    prev = val;
                }
                else{
                    prev = val;
                }
                cho = ch;
                hs.add(val);
                // mp.put(ch, val);
            }
            List<String> ans = new ArrayList<>();
            for(int num : queries) {
                if(hs.contains(num))
                    ans.add("Yes");
                else
                    ans.add("No");
            }
            return ans;
    
        }
    
  • + 7 comments

    c++

    
    
    set<int> weight;
    int count = 1;
    for (int i = 0; i < s.size(); ++i) {
        int currentWeight = (s[i] - 'a' + 1);
        if (i > 0 && s[i] == s[i - 1]) {
            count++;
        } else {
            count = 1;
        }
        weight.insert(count * currentWeight);
    }
    vector<string> result;
    for (int q : queries) {
        if (weight.find(q) != weight.end()) {
            result.push_back("Yes");
        } else {
            result.push_back("No");
        }
    }
    return result;
    

    }

  • + 0 comments

    c++ vector weightedUniformStrings(string s, vector queries) { set weight; int count = 1; for (int i = 0; i < s.size(); ++i) { int currentWeight = (s[i] - 'a' + 1); if (i > 0 && s[i] == s[i - 1]) { count++; } else { count = 1; } weight.insert(count * currentWeight); } vector result; for (int q : queries) { if (weight.find(q) != weight.end()) { result.push_back("Yes"); } else { result.push_back("No"); } } return result; }

  • + 0 comments

    Java:

     static int getWeight(char c){
        return (int) (c - 96);   
        } 
       
       public static List<String> weightedUniformStrings(String s, List<Integer> queries) {
        List<String> result = new ArrayList<>();
        List<Integer> weights = new ArrayList<>();
        HashMap<Character, Integer> map = new HashMap<>();
        int current_len = 1;
        int n = s.length();
        
        for(int i = 0; i < n; i++){
            char c = s.charAt(i);
            if(!map.containsKey(c))
              map.put(c, 1);
            else{
              if( c == s.charAt(i - 1) ){
                current_len ++;
                if( i == n-1 || c != s.charAt(i + 1) ){
                    map.put(c, Math.max(current_len, map.get(c)) );
                    current_len = 1;
                    }
                }     
              }
            }
        
        for(char key:map.keySet()){
            int value = map.get(key);
            int weight = getWeight(key);
            for(int i = 1; i <= value; i++){
                weights.add( i * weight );
                }
            }
        
        for(int query:queries){
            if(weights.contains(query))
              result.add("Yes");
            else
              result.add("No");
            
            }
        return result;
        }
    
  • + 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]