From d3fcf656e53fa119f8c2507b19046703b98f1994 Mon Sep 17 00:00:00 2001 From: leonardchinonso <36096513+leonardchinonso@users.noreply.github.com> Date: Sat, 17 Apr 2021 01:05:18 +0100 Subject: [PATCH] Update 05.py --- 1-100q/05.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) 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 +