Skip to content

Commit 0bcd8f9

Browse files
committed
python 1397 added
1 parent dad857b commit 0bcd8f9

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

python/1397-find-all-good-strings.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution:
2+
def findGoodStrings(self, n: int, s1: str, s2: str, evil: str) -> int:
3+
a = ord('a')
4+
z = ord('z')
5+
6+
arr_e = list(map(ord, evil))
7+
len_e = len(evil)
8+
next = [0] * len_e
9+
10+
for i in range(1, len_e):
11+
j = next[i - 1]
12+
while j > 0 and evil[i] != evil[j]:
13+
j = next[j - 1]
14+
if evil[i] == evil[j]:
15+
next[i] = j + 1
16+
17+
def good(s):
18+
arr = list(map(ord, s))
19+
len_a = len(arr)
20+
21+
@cache
22+
def f(i, skip, reach, e):
23+
if e == len_e:
24+
return 0
25+
if i == len_a:
26+
return 0 if skip else 1
27+
28+
limit = arr[i] if reach else z
29+
ans = 0
30+
31+
if skip:
32+
ans += f(i + 1, True, False, 0)
33+
34+
for c in range(a, limit + 1):
35+
ee = e
36+
while ee > 0 and arr_e[ee] != c:
37+
ee = next[ee - 1]
38+
39+
if arr_e[ee] == c:
40+
ee += 1
41+
42+
ans += f(i + 1, False, reach and c == limit, ee)
43+
44+
return ans % int(1e9 + 7)
45+
46+
return f(0, True, True, 0)
47+
48+
return (good(s2) - good(s1) + int(evil not in s1)) % int(1e9 + 7)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy