diff --git a/1-100q/05.py b/1-100q/05.py index 1cdd9b9..bbbb9e3 100644 --- a/1-100q/05.py +++ b/1-100q/05.py @@ -71,4 +71,31 @@ def expand(s, left, right): start = index - (length-1)/2 end = index +length/2 - return s[start:end+1] \ No newline at end of file + return s[start:end+1] + +''' +Alternative Solution +''' + +class Solution: + def longestPalindrome(self, s: str) -> str: + if not len(s): return 0 + + self.longest = "" + i = 0 + while i < len(s): + self.expandFromMiddle(i, i, s) + self.expandFromMiddle(i, i+1, s) + i += 1 + + return self.longest + + def expandFromMiddle(self, left, right, word): + newWord = "" + while left >= 0 and right < len(word) and word[left] == word[right]: + newWord = word[left:right+1] + left -= 1 + right += 1 + if (len(self.longest) < len(newWord)): + self.longest = newWord +