Skip to content

Commit 04fcf31

Browse files
committed
Week 15: 5. Longest Palindromic Substring Solution
1 parent 52dfd63 commit 04fcf31

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 5. Longest Palindromic Substring
2+
# https://leetcode.com/problems/longest-palindromic-substring/
3+
4+
5+
class Solution:
6+
def longestPalindrome(self, s: str) -> str:
7+
#Expand Around Center
8+
res = ""
9+
resLen = 0
10+
11+
for i in range(len(s)):
12+
# odd length
13+
l, r = i, i
14+
while l >= 0 and r < len(s) and s[l] == s[r]:
15+
if (r - l + 1) > resLen:
16+
res = s[l:r+1]
17+
resLen = r - l + 1
18+
l -= 1
19+
r += 1
20+
21+
# even length
22+
l, r = i, i + 1
23+
while l >= 0 and r < len(s) and s[l] == s[r]:
24+
if (r - l + 1) > resLen:
25+
res = s[l:r+1]
26+
resLen = r - l + 1
27+
l -= 1
28+
r += 1
29+
30+
return res

0 commit comments

Comments
 (0)