-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem.py
More file actions
39 lines (33 loc) · 1.12 KB
/
Copy pathproblem.py
File metadata and controls
39 lines (33 loc) · 1.12 KB
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
#!/usr/bin/env python3
from string.kmp.template import KMP
class Solution:
@staticmethod
def lc28_str_str():
"""
link: https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/
tag: string | kmp | two pointers
"""
haystack, needle = "sadbutsad", "sad"
kmp = KMP(haystack, needle)
print(kmp.matches[0] if kmp.matches else -1)
@staticmethod
def lc796_rotate_string():
"""
link: https://leetcode.cn/problems/rotate-string/
tag: string | kmp | string matching
"""
s, goal = "abcde", "cdeab"
kmp = KMP(s + s, goal)
print(len(goal) == len(s) and bool(kmp.matches))
@staticmethod
def lc459_repeated_substring():
"""
link: https://leetcode.cn/problems/repeated-substring-pattern/
tag: string | kmp | next array
"""
s = "abab"
n = len(s)
nxt = KMP("", "").computeNxt(s)
# If s is composed of repeated substrings, the last nxt value gives the period
period = n - nxt[-1]
print(period > 0 and n % period == 0)