Weighted Uniform Strings

  • + 0 comments

    python solution

    def weightedUniformStrings(s, queries):
        # Write your code here
        dp = []
        for i in range(len(s)):
            weight = ord(s[i]) - 96
            if i == 0 or (s[i] != s[i-1]):
                dp.append(weight)
            else:
                dp.append(weight + dp[i-1])
        ans = []
        dp = set(dp)
        for x in queries:
            if x in dp:
                ans.append("Yes")
            else:
                ans.append("No")
        return ans