Created
April 11, 2024 23:09
-
-
Save jesuscmadrigal/c52e3b4d0cd18543a741220d1d074419 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def longestPalindrome(self, s: str) -> str: | |
result, resultLen = "",0 | |
for i in range(len(s)): | |
result, resultLen = computePalindrome(i,i, s, result, resultLen) | |
result, resultLen = computePalindrome(i,i+1, s, result, resultLen) | |
return result | |
def computePalindrome(left, right, string, result, resultLen): | |
while left >= 0 and right < len(string) and string[left] == string[right]: | |
if (right - left + 1) > resultLen: | |
result = string[left:right+1] | |
resultLen = right - left + 1 | |
left -= 1 | |
right += 1 | |
return [result, resultLen] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment