-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC_1397.py
55 lines (38 loc) · 1.53 KB
/
LC_1397.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from functools import lru_cache
class Solution:
def findGoodStrings(self, n: int, s1: str, s2: str, evil: str) -> int:
mod = 10 ** 9 + 7
def get_kmp_next(s):
next_arr = [0] * len(s)
i, j = 1, 0
while i < len(s):
while j > 0 and s[i] != s[j]:
j = next_arr[j - 1]
if s[i] == s[j]:
j += 1
next_arr[i] = j
i += 1
return next_arr
kmp_next = get_kmp_next(evil)
@lru_cache(None)
def solve(idx, is_prefix_s1, is_prefix_s2, prefix_evil):
if prefix_evil == len(evil):
return 0
if idx == n:
return 1
total = 0
first = ord(s1[idx]) if is_prefix_s1 else ord('a')
last = ord(s2[idx]) if is_prefix_s2 else ord('z')
for c in range(first, last + 1):
char = chr(c)
next_prefix_s1 = is_prefix_s1 and c == first
next_prefix_s2 = is_prefix_s2 and c == last
next_prefix_evil = prefix_evil
while next_prefix_evil and evil[next_prefix_evil] != char:
next_prefix_evil = kmp_next[next_prefix_evil - 1]
if char == evil[next_prefix_evil]:
next_prefix_evil += 1
total += solve(idx + 1, next_prefix_s1, next_prefix_s2, next_prefix_evil)
total %= mod
return total
return solve(0, True, True, 0)