Search This Blog




weighted uniform string hacker rank solution javascript



Please refer this link for problem statement click here




Javascript program of weighted uniform string hackerrank solution


function weightedUniformStrings(s, queries) {
    let result = [], answers = {}, count, key, pivot, code;
    
    for (let i = 0; i < s.length; i++) {
        count = 1;        
        code = s[i].charCodeAt();
        pivot = s.charAt(i);
        key = (count * (code - 97 + 1));
        answers[key] = key;
        
        while (pivot == s.charAt(i + 1) && i < s.length - 1) {
            count++;
            key = (count * (code - 97 + 1));
            answers[key] = key;
            i++;
        }
    }

    for (let i = 0; i < queries.length; i++) {
        if (answers[queries[i]]) {
            result.push('Yes');
        else {
            result.push('No');
        }
    }
    
    return
result;
}




Time Complexity : O(n^2)

Space Complexity : O(n)



Post a Comment

0 Comments