Weighted Uniform Strings

  • + 0 comments
    function weightedUniformStrings(s, queries) {
        const validWeights = new Set();
        let currentChar = '';
        let currentWeight = 0;
        let count = 0;
    
        for (let i = 0; i < s.length; i++) {
            if (s[i] === currentChar) {
                count++;
            } else {
                currentChar = s[i];
                count = 1;
                currentWeight = s.charCodeAt(i) - 96; // 'a' has ASCII value 97
            }
    
            validWeights.add(currentWeight * count);
        }
    
        return queries.map(q => validWeights.has(q) ? 'Yes' : 'No');
    }