-
Notifications
You must be signed in to change notification settings - Fork 43
/
palindromic-substrings.py
296 lines (268 loc) · 7.86 KB
/
palindromic-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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""
647. Palindromic Substrings
Medium
Given a string s, return the number of palindromic substrings in it.
A string is a palindrome when it reads the same backward as forward.
A substring is a contiguous sequence of characters within the string.
Example 1:
Input: s = "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".
Example 2:
Input: s = "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
Constraints:
1 <= s.length <= 1000
s consists of lowercase English letters.
"""
# V0
# IDEA : BRUTE FORCE
class Solution(object):
def countSubstrings(self, s):
count = 0
# NOTE: since i from 0 to len(s) - 1, so for j we need to "+1" then can get go throgh all elements in str
for i in range(len(s)):
# Note : for j we need to "+1"
for j in range(i+1, len(s)+1):
if s[i:j] == s[i:j][::-1]:
count += 1
return count
# V0'
# IDEA : BRUTE FORCE
class Solution(object):
def countSubstrings(self, s):
count = 0
for i in range(len(s)):
for j in range(i, len(s)):
if s[i:j + 1] == s[i:j + 1][::-1]:
count += 1
return count
# V0''
# IDEA : TWO POINTERS (similar as LC 005)
class Solution(object):
def countSubstrings(self, s):
count = 0
for i in range(len(s)):
# for every single character
count += 1
# case 1) palindromic substrings length is odd
left = i - 1
right = i + 1
while left >= 0 and right < len(s) and s[left] == s[right]:
count += 1
left -= 1
right += 1
# case 2) palindromic substrings length is even
left = i - 1
right = i
while left >= 0 and right < len(s) and s[left] == s[right]:
count += 1
left -= 1
right += 1
return count
# V0''
# IDEA : TWO POINTERS
# https://leetcode.com/problems/palindromic-substrings/discuss/1041760/Python-Easy-Solution-Beats-85
# https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/String/longest-palindromic-substring.py
class Solution:
def countSubstrings(self, s):
ans = 0
for i in range(len(s)):
# odd case
# example : s = "abc"
# -> so have to set (l, r) at (2,2) (index)
ans += self.helper(s, i, i)
# even case
# example : s = "abcd"
# -> so have to set (l, r) at (1,2) (index)
ans += self.helper(s, i, i + 1)
return ans
def helper(self, s, l, r):
ans = 0
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
ans += 1
return ans
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79433960
# IDEA : GREEDY
class Solution(object):
def countSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
count = 0
for i in range(len(s)):
for j in range(i, len(s)):
if s[i:j + 1] == s[i:j + 1][::-1]:
count += 1
return count
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79433960
class Solution(object):
def countSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
count = 0
for i in range(len(s)):
count += 1
#length of palindromic substrings is odd
left = i - 1
right = i + 1
while left >= 0 and right < len(s) and s[left] == s[right]:
count += 1
left -= 1
right += 1
#length of palindromic substrings is even
left = i
right = i + 1
while left >= 0 and right < len(s) and s[left] == s[right]:
count += 1
left -= 1
right += 1
return count
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/79433960
# IDEA : DP
class Solution(object):
def countSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
n = len(s)
count = 0
start, end, maxL = 0, 0, 0
dp = [[0] * n for _ in range(n)]
for i in range(n):
for j in range(i):
dp[j][i] = (s[j] == s[i]) & ((i - j < 2) | dp[j + 1][i - 1])
if dp[j][i]:
count += 1
dp[i][i] = 1
count += 1
return count
# V1'''
# IDEA : Check All Substrings
# https://leetcode.com/problems/palindromic-substrings/solution/
# JAVA
# class Solution {
# private boolean isPalindrome(String s, int start, int end) {
# while (start < end) {
# if (s.charAt(start) != s.charAt(end))
# return false;
#
# ++start;
# --end;
# }
#
# return true;
# }
#
# public int countSubstrings(String s) {
# int ans = 0;
#
# for (int start = 0; start < s.length(); ++start)
# for (int end = start; end < s.length(); ++end)
# ans += isPalindrome(s, start, end) ? 1 : 0;
#
# return ans;
# }
# }
# V1'''''
# IDEA : DP
# https://leetcode.com/problems/palindromic-substrings/solution/
# JAVA
# class Solution {
# public int countSubstrings(String s) {
# int n = s.length(), ans = 0;
#
# if (n <= 0)
# return 0;
#
# boolean[][] dp = new boolean[n][n];
#
# // Base case: single letter substrings
# for (int i = 0; i < n; ++i, ++ans)
# dp[i][i] = true;
#
# // Base case: double letter substrings
# for (int i = 0; i < n - 1; ++i) {
# dp[i][i + 1] = (s.charAt(i) == s.charAt(i + 1));
# ans += (dp[i][i + 1] ? 1 : 0);
# }
#
# // All other cases: substrings of length 3 to n
# for (int len = 3; len <= n; ++len)
# for (int i = 0, j = i + len - 1; j < n; ++i, ++j) {
# dp[i][j] = dp[i + 1][j - 1] && (s.charAt(i) == s.charAt(j));
# ans += (dp[i][j] ? 1 : 0);
# }
#
# return ans;
# }
# }
# V1''''''
# IDEA : Expand Around Possible Centers
# https://leetcode.com/problems/palindromic-substrings/solution/
# JAVA
# class Solution {
# public int countSubstrings(String s) {
# int ans = 0;
#
# for (int i = 0; i < s.length(); ++i) {
# // odd-length palindromes, single character center
# ans += countPalindromesAroundCenter(s, i, i);
#
# // even-length palindromes, consecutive characters center
# ans += countPalindromesAroundCenter(s, i, i + 1);
# }
#
# return ans;
# }
#
# private int countPalindromesAroundCenter(String ss, int lo, int hi) {
# int ans = 0;
#
# while (lo >= 0 && hi < ss.length()) {
# if (ss.charAt(lo) != ss.charAt(hi))
# break; // the first and last characters don't match!
#
# // expand around the center
# lo--;
# hi++;
#
# ans++;
# }
#
# return ans;
# }
# }
# V2
# Time: O(n)
# Space: O(n)
class Solution(object):
def countSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
def manacher(s):
s = '^#' + '#'.join(s) + '#$'
P = [0] * len(s)
C, R = 0, 0
for i in range(1, len(s) - 1):
i_mirror = 2*C-i
if R > i:
P[i] = min(R-i, P[i_mirror])
while s[i+1+P[i]] == s[i-1-P[i]]:
P[i] += 1
if i+P[i] > R:
C, R = i, i+P[i]
return P
return sum((max_len+1)//2 for max_len in manacher(s))