Skip to content

Commit

Permalink
Merge pull request neetcode-gh#3049 from coopers/coopers-0005
Browse files Browse the repository at this point in the history
Update 0005-longest-palindromic-substring.py
  • Loading branch information
MHamiid authored Oct 7, 2023
2 parents 5b65798 + 5a4d9dd commit 41307ff
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions python/0005-longest-palindromic-substring.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
class Solution:
def longestPalindrome(self, s: str) -> str:
res = ""
resLen = 0

for i in range(len(s)):
# odd length
l, r = i, i
while l >= 0 and r < len(s) and s[l] == s[r]:
if (r - l + 1) > resLen:
res = s[l : r + 1]
resLen = r - l + 1
l -= 1
r += 1
for l, r in ((i,i), (i,i+1)): # odd and even lengths
while l >= 0 and r < len(s) and s[l] == s[r]:
if (r - l + 1) > len(res):
res = s[l:r + 1]
l -= 1
r += 1

# even length
l, r = i, i + 1
while l >= 0 and r < len(s) and s[l] == s[r]:
if (r - l + 1) > resLen:
res = s[l : r + 1]
resLen = r - l + 1
l -= 1
r += 1

return res
return res

0 comments on commit 41307ff

Please sign in to comment.