forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distinct-echo-substrings.py
101 lines (92 loc) · 3.52 KB
/
distinct-echo-substrings.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Time: O(n^2 + d), d is the duplicated of result substrings size
# Space: O(r), r is the size of result substrings set
class Solution(object):
def distinctEchoSubstrings(self, text):
"""
:type text: str
:rtype: int
"""
def KMP(text, l, result):
prefix = [-1]*(len(text)-l)
j = -1
for i in xrange(1, len(prefix)):
while j > -1 and text[l+j+1] != text[l+i]:
j = prefix[j]
if text[l+j+1] == text[l+i]:
j += 1
prefix[i] = j
if (j+1) and (i+1) % ((i+1) - (j+1)) == 0 and \
(i+1) // ((i+1) - (j+1)) % 2 == 0:
result.add(text[l:l+i+1])
return len(prefix)-(prefix[-1]+1) \
if prefix[-1]+1 and len(prefix) % (len(prefix)-(prefix[-1]+1)) == 0 \
else float("inf")
result = set()
i, l = 0, len(text)-1
while i < l: # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabcdefabcdefabcdef
l = min(l, i + KMP(text, i, result));
i += 1
return len(result)
# Time: O(n^2 + d), d is the duplicated of result substrings size
# Space: O(r), r is the size of result substrings set
class Solution2(object):
def distinctEchoSubstrings(self, text):
"""
:type text: str
:rtype: int
"""
result = set()
for l in xrange(1, len(text)//2+1):
count = sum(text[i] == text[i+l] for i in xrange(l))
for i in xrange(len(text)-2*l):
if count == l:
result.add(text[i:i+l])
count += (text[i+l] == text[i+l+l]) - (text[i] == text[i+l])
if count == l:
result.add(text[len(text)-2*l:len(text)-2*l+l])
return len(result)
# Time: O(n^2 + d), d is the duplicated of result substrings size
# Space: O(r), r is the size of result substrings set
class Solution3(object):
def distinctEchoSubstrings(self, text):
"""
:type text: str
:rtype: int
"""
MOD = 10**9+7
D = 27 # a-z and ''
result = set()
for i in xrange(len(text)-1):
left, right, pow_D = 0, 0, 1
for l in xrange(1, min(i+2, len(text)-i)):
left = (D*left + (ord(text[i-l+1])-ord('a')+1)) % MOD
right = (pow_D*(ord(text[i+l])-ord('a')+1) + right) % MOD
if left == right: # assumed no collision
result.add(left)
pow_D = (pow_D*D) % MOD
return len(result)
# Time: O(n^3 + d), d is the duplicated of result substrings size
# Space: O(r), r is the size of result substrings set
class Solution_TLE(object):
def distinctEchoSubstrings(self, text):
"""
:type text: str
:rtype: int
"""
def compare(text, l, s1, s2):
for i in xrange(l):
if text[s1+i] != text[s2+i]:
return False
return True
MOD = 10**9+7
D = 27 # a-z and ''
result = set()
for i in xrange(len(text)):
left, right, pow_D = 0, 0, 1
for l in xrange(1, min(i+2, len(text)-i)):
left = (D*left + (ord(text[i-l+1])-ord('a')+1)) % MOD
right = (pow_D*(ord(text[i+l])-ord('a')+1) + right) % MOD
if left == right and compare(text, l, i-l+1, i+1):
result.add(text[i+1:i+1+l])
pow_D = (pow_D*D) % MOD
return len(result)