-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestPalindromicSubstring.py
401 lines (331 loc) · 12.6 KB
/
LongestPalindromicSubstring.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#5. Longest Palindromic Substring
#Medium
#25.8K
#1.5K
#Companies
#Given a string s, return the longest
#palindromic
#substring
# in s.
#Example 1:
#Input: s = "babad"
#Output: "bab"
#Explanation: "aba" is also a valid answer.
#Example 2:
#Input: s = "cbbd"
#Output: "bb"
#Constraints:
#1 <= s.length <= 1000
#s consist of only digits and English letters.
#My Solution (Python):
#We are expanding outwards from the center by comparing left and right values. If left and right values match, we keep expanding outwards until characters either 1. no longer match or 2. reach the boundaries of the string
class Solution(object):
def longestPalindrome(self, s):
def helper(left, right): #helper function keeps track of the longest palindrome we have found so far
while (left >= 0 and right < len(s) and s[left] == s[right]): #boundary checks to ensure left dosen't go out of bounds to the left side of the string and right dosen't go out of bounds to the right side of the string
left -= 1
right += 1
return s[left + 1: right] #ensures that the character at index left is not included in the substring because it is the character that caused the loop to exit and no longer satisfies the palindrome condition. By excluding the character at index left, the returned substring represents the longest palindrome substring without including the expanding character.
result = "" #initializes result as an empty string to set an initial value for result before the loop starts. since no palindromic substring has been found yet, result is, initially, set to an empty substring. As the loop progresses and palindromic substrings are found, result value will be updated to store the longest palindromic substring found up to that point
for i in range(len(s)):
test = helper(i, i) #single character as center aka s is an odd number of characters
if len(test) > len(result): #result keeps track of the longest palindromic substring found so far, so if test is greater than result, result updates to test to store the new longest palindromic substring so far
result = test
test = helper(i, i + 1) #two adjacent characters as center aka s is an even number of characters
if len(test) > len(result):
result = test
return result #at the end, result will contain the longest palindromic substring found in the given string s
#1/3/24 refresher:
class Solution:
def longestPalindrome(self, s):
def f(l, r, s):
#making sure we are inside the string with the two pointers and that the characters from both pointers are the same. If so, we expand outward forever until one of these conditions is broken to find an even longer palindromic substring.
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
#if the while loop is broken meaning we are outside the string or one side is nor equal to the other side character, then we need to step backwards and include the substring that was valid by moving left forward one and right backward one from the point left and right were at when this while loop was violated and return that to be compared
return s[l + 1: r]
res = ""
for char in range(len(s)):
first = f(char, char, s)
if len(first) > len(res):
res = first
second = f(char, char + 1, s)
if len(second) > len(res):
res = second
return res
#1/6/24 refresher:
class Solution:
def longestPalindrome(self, s):
def dfs(l, r, s):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
return s[l + 1: r]
#the question asks for the longest palindromic substring itself, so either one works if both are a tie. It dosen't ask for the length of the longest palindomic substring.
res = ""
for char in range(len(s)):
first = dfs(char, char, s)
if len(first) > len(res):
res = first
second = dfs(char, char + 1, s)
if len(second) > len(res):
res = second
return res
#1/16/24 refresher:
class Solution:
def longestPalindrome(self, s):
def dfs(l, r, s):
while l >= 0 and r < len(s) and s[l] == s[r]:
#expanding outwad to find a longer palindrome window than before if we are in bounds and the letters both pointers are pointing to are the same (think reverse staircase)
l -= 1
r += 1
return s[l + 1: r]
res = ""
#remember that a palindrome can occur starting anywhere in the string, not just from the middle!!!!!
for char in range(len(s)):
first = dfs(char, char, s)
if len(first) > len(res):
res = first
second = dfs(char, char + 1, s)
if len(second) > len(res):
res = second
return res
#1/18/24 refresher:
class Solution:
def longestPalindrome(self, s):
def dfs(l, r, s):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
return s[l + 1: r]
res = ""
for char in range(len(s)):
first = dfs(char, char, s)
if len(first) > len(res):
res = first
second = dfs(char, char + 1, s)
if len(second) > len(res):
res = second
return res
#2/4/24 refresher practice:
class Solution:
def longestPalindrome(self, s):
#we want the longest substring itself, so a string is the return type and not an integer
def expandfromanywhere(l, r, s):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
#when the while loop evaluates to False, we terminate the while loop, and we return the last VALID substring back to either same or different
return s[l + 1: r]
res = ""
for char in range(len(s)):
same = expandfromanywhere(char, char, s)
if len(same) > len(res):
res = same
different = expandfromanywhere(char, char + 1, s)
if len(different) > len(res):
res = different
return res
#2/20/24:
class Solution:
def longestPalindrome(self, s):
def f(l, r, s):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
return s[l + 1: r]
res = ""
for char in range(len(s)):
first = f(char, char, s)
if len(first) > len(res):
res = first
second = f(char, char + 1, s)
if len(second) > len(res):
res = second
return res
#2/26/24:
class Solution:
def longestPalindrome(self, s: str) -> str:
def f(l, r, s):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1 #keep expanding outward because we are looking for a longer palindromic substring
r += 1
return s[l + 1: r]
res = ""
for char in range(len(s)):
first = f(char, char, s) #palindromic substrings can start anywhere in the string, not just in the middle, so expand one and two units, inching forward
if len(first) > len(res):
res = first
second = f(char, char + 1, s)
if len(second) > len(res):
res = second
return res
#3/9/24:
class Solution:
def longestPalindrome(self, s):
def f(l, r, s):
while l >= 0 and r < len(s) and s[l] == s[r]: #while, not if!!!!!
l -= 1
r += 1
return s[l + 1: r]
res = ""
for char in range(len(s)):
first = f(char, char, s)
if len(first) > len(res):
res = first
second = f(char, char + 1, s)
if len(second) > len(res):
res = second
return res
#3/15/24:
class Solution:
def longestPalindrome(self, s):
def f(l, r, s):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
return s[l + 1:r]
res = ""
for char in range(len(s)):
first = f(char, char, s)
if len(first) > len(res):
res = first
second = f(char, char + 1, s)
if len(second) > len(res):
res = second
return res
#3/24/24:
class Solution:
def longestPalindrome(self, s):
def f(l, r, s):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
return s[l + 1: r]
res = ""
for char in range(len(s)):
first = f(char, char, s)
if len(first) > len(res): #don't forget len() because you can't just compare two strings directly without len!
res = first
second = f(char, char + 1, s)
if len(second) > len(res):
res = second
return res
#4/15/24:
class Solution:
def longestPalindrome(self, s: str) -> str:
def f(l, r, s):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
return s[l + 1: r]
res = ""
for i in range(len(s)):
first = f(i, i, s)
if len(first) > len(res):
res = first
second = f(i, i + 1, s)
if len(second) > len(res):
res = second
return res
#5/11/24 refresher:
class Solution:
def longestPalindrome(self, s):
def f(l, r, s):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
return s[l + 1:r]
res = ""
for i in range(len(s)):
first = f(i, i, s)
if len(first) > len(res):
res = first
second = f(i, i + 1, s)
if len(second) > len(res):
res = second
return res
#6/5/24 review:
class Solution:
def longestPalindrome(self, s):
def f(l, r , s):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
return s[l + 1: r]
res = ""
for i in range(len(s)):
first = f(i, i, s)
if len(first) > len(res):
res = first
second = f(i, i + 1, s)
if len(second) > len(res):
res = second
return res
#7/14/24 refresher:
class Solution:
def longestPalindrome(self, s: str) -> str:
def f(l, r):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
return s[l + 1:r]
res = ""
for i in range(len(s)):
first = f(i, i)
if len(first) > len(res):
res = first
second = f(i, i + 1)
if len(second) > len(res):
res = second
return res
#9/8/24 review:
class Solution:
def longestPalindrome(self, s: str) -> str:
def f(l, r, s):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
return s[l + 1: r]
res = ""
for i in range(len(s)):
first = f(i, i, s)
if len(first) > len(res):
res = first
second = f(i, i + 1, s)
if len(second) > len(res):
res = second
return res
#10/18/24 review:
class Solution:
def longestPalindrome(self, s: str) -> str:
def f(l, r, s):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
return s[l + 1: r]
res = ""
for i in range(len(s)):
first = f(i, i, s)
if len(first) > len(res):
res = first
second = f(i, i + 1, s)
if len(second) > len(res):
res = second
return res
#11/5/24 review (my own solution using python3):
class Solution:
def longestPalindrome(self, s: str) -> str:
res = []
maxlen = 0
for i in range(len(s)):
for j in range(i, len(s)):
substr = s[i: j + 1]
if substr == substr[::-1]:
res.append(substr)
maxlen = max(maxlen, len(substr))
for r in res:
if len(r) == maxlen:
return r
return ""