Skip to content

Commit 0fbc611

Browse files
committed
feat: Solve longest-palindromic-substring problem
1 parent 8c46d00 commit 0fbc611

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def longestPalindrome(self, s: str) -> str:
3+
# if len(s) <= 2:
4+
# return s[0]
5+
6+
# for i in range(2, len(s)):
7+
# for k in range(len(s)-i):
8+
# if s[k:k+i] == s[k:k+i][::-1]:
9+
# return s[k:k+i]
10+
11+
max_s, max_e = 0, 0
12+
13+
for i in range(len(s)):
14+
start, end = i, i
15+
while 0 <= start and end < len(s) and s[start] == s[end]:
16+
if max_e - max_s < end - start:
17+
max_s, max_e = start, end
18+
start, end = start - 1, end + 1
19+
20+
start, end = i, i + 1
21+
while 0 <= start and end < len(s) and s[start] == s[end]:
22+
if max_e - max_s < end - start:
23+
max_s, max_e = start, end
24+
start, end = start - 1, end + 1
25+
26+
return s[max_s : max_e + 1]

0 commit comments

Comments
 (0)