Участник:UlitinAleksander/find-all-good-strings

Материал из DISCOPAL
Перейти к: навигация, поиск

python3

https://leetcode.com/problems/find-all-good-strings/

class Solution:
    def findGoodStrings(self, n, s1, s2, evil):
        M = 10 ** 9 + 7
        m = len(evil)
        memo = {}
        # use kmp to fetch the length of longest common prefix
        dfa = [0] * m
        l = 0
        for r in range(1, m):
            while l and evil[l] != evil[r]:
                l = dfa[l - 1]
            l += evil[l] == evil[r]
            dfa[r] = l
 
        def dfs(i, x, bound):
            if x == m:
                return 0
            if i == n:
                return 1
            if (i, x, bound) not in memo:
                cnt = 0
                lo = ord('a' if bound & 1 else s1[i])  # "be*" -> {"be*", "ca*", ..}, 'b' when bound bit = ?0
                hi = ord('z' if bound & 2 else s2[i])  # "do*" -> {"cz*", "do*", ..}, 'd' when bound bit = 0?
                for j, c in enumerate(chr(o) for o in range(lo, hi + 1)):
                    y = x
                    while y and evil[y] != c:
                        y = dfa[y - 1]
                    y += evil[y] == c
                    cnt = (cnt + dfs(i + 1, y, bound | (j > 0) | (j < hi - lo) << 1)) % M
                memo[i, x, bound] = cnt
            return memo[i, x, bound]
 
        return dfs(0, 0, 0)