Weighted Uniform Strings

Sort by

recency

|

788 Discussions

|

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

    Simple C Solution;

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    int main() {

    int n, x, i, j, k;
    int a[27] = {0};
    int f = 1;
    char s[100000];
    
    fgets(s, sizeof(s), stdin);
    s[strcspn(s, "\n")] = 0;
    
    for (i = 0; i < 27; ++i) {
        a[i] = 0;
    }
    
    for (i = 0; s[i] != '\0'; ++i) {
    
        if (s[i] == s[i + 1]) {
            ++f;
        } 
        else {
    
            if (f > a[s[i] - 'a']) {
                a[s[i] - 'a'] = f;
            }
            f = 1;
        }
    }
    
    if (f > a[s[i - 1] - 'a']) {
        a[s[i - 1] - 'a'] = f;
    }
    
    scanf("%d", &n);
    for (k = 0; k < n; ++k) {
        scanf("%d", &x);
    
        bool found = false;
        for (i = 1; i <= 26; ++i) {
    
            if (x % i == 0) {
    
                if (x / i <= a[i - 1]) {
                    found = true;
                    break;
                }
            }
        }
    
        if (found) {
            printf("Yes\n");
        } 
        else {
            printf("No\n");
        }
    }
    
    return 0;
    

    }

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