Weighted Uniform Strings

  • + 0 comments

    Python

    def weightedUniformStrings(s, queries):
        weights = []
        
        for i, char in enumerate(s):
            #compare unicode value of char to unicode of 'a' and add 1 to find weight
            weight = ord(char) - ord('a') + 1
            if i > 0 and s[i-1] == char:
                weight += weights[-1]
            weights.append(weight)
        
        #get a set of the unique weights
        weights = set(weights)
        
        return ['Yes' if query in weights else 'No' for query in queries]