Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution:
def longestPalindrome(self, s: str) -> str:
if len(s) < 2:
return s

start = 0
max_len = 1

def expand(left: int, right: int) -> None:
nonlocal start, max_len
# Expand outward while the substring stays a palindrome
while left >= 0 and right < len(s) and s[left] == s[right]:
left -= 1
right += 1
# Loop exits one step too far, so the palindrome is s[left+1 : right]
length = right - left - 1
if length > max_len:
max_len = length
start = left + 1

for i in range(len(s)):
expand(i, i) # odd-length center
expand(i, i + 1) # even-length center

return s[start:start + max_len]
Loading