Weighted Uniform Strings

  • + 0 comments

    simple O(N) solution in Python:

    def weightedUniformStrings(s, queries):
        # Write your code here
        weights = set()
        for i, c in  enumerate(s):
            u = ord(c) - ord('a') + 1
            if i > 0 and c == s[i-1]:
                w += u
            else:
                w = u
            if w not in weights:
                weights.add(w)
        result = []       
        for q in queries:
            if q in weights:
                result.append("Yes")
            else: 
                result.append("No")
        return result