MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / Solution

Class Solution

Python/5_LongestPalindromicSubstring.py:22–46  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20
21
22class Solution:
23 def longestPalindrome(self, s: str) -> str:
24 longest = ""
25 size = len(s)
26 for i in range(size):
27 #Odd Palindrome
28 left,right = i,i
29 ls = ""
30 while 0<=left<size and 0<=right<size and s[left]==s[right]:
31 if left==right:
32 ls = s[left]
33 else:
34 ls = s[left] + ls + s[right]
35 left-=1
36 right+=1
37 #Even Palindrome
38 left,right = i,i+1
39 rs = ""
40 while 0<=left<size and 0<=right<size and s[left]==s[right]:
41 rs = s[left] + rs + s[right]
42 left-=1
43 right+=1
44 temp = ls if len(ls)>len(rs) else rs
45 longest = temp if len(temp)>len(longest) else longest
46 return longest

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected