Weighted Uniform Strings

  • + 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')
    }