Created
September 17, 2019 23:22
-
-
Save DataSolveProblems/10875644219b0f74e95c990a4ec1bb25 to your computer and use it in GitHub Desktop.
This file contains hidden or 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: | |
longest_str = '' | |
n = [len(s) - 1] | |
for diff in range(1, len(s)): | |
n.append(n[0] + diff) | |
n.append(n[0] - diff) | |
for i in n: | |
if (min(i + 1, 2 * len(s) - 1 - i) <= len(longest_str)): | |
break | |
if i % 2 == 0: | |
left, right = (i // 2) - 1, (i // 2) + 1 | |
else: | |
left, right = i // 2, (i // 2) + 1 | |
while left >= 0 and right < len(s) and s[left] == s[right]: | |
left -= 1 | |
right += 1 | |
if right - left - 1 > len(longest_str): | |
longest_str = s[left + 1: right] | |
return longest_str |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment