Skip to content

Commit 9c6b33e

Browse files
committed
[Leo] 10th Week solutions (Last Q)
1 parent 0f2057e commit 9c6b33e

File tree

1 file changed

+23
-0
lines changed
  • longest-palindromic-substring

1 file changed

+23
-0
lines changed

longest-palindromic-substring/Leo.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def longestPalindrome(self, s: str) -> str:
3+
if s == s[::-1]:
4+
return s
5+
6+
start, max_length = 0, 1
7+
8+
for i in range(1, len(s)):
9+
odd_s = i - max_length - 1
10+
even_s = i - max_length
11+
odd_p = s[odd_s:i + 1]
12+
even_p = s[even_s:i + 1]
13+
14+
if odd_s >= 0 and odd_p == odd_p[::-1]:
15+
start = odd_s
16+
max_length += 2
17+
elif even_p == even_p[::-1]:
18+
start = even_s
19+
max_length += 1
20+
21+
return s[start:start + max_length]
22+
23+
## TC: O(n^2), SC: O(1)

0 commit comments

Comments
 (0)