Skip to content

Commit 3f74675

Browse files
committed
Week 15
1 parent 0d2d732 commit 3f74675

File tree

1 file changed

+28
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)