Skip to content

Commit

Permalink
Time: 4 ms (53.01%), Space: 4.4 MB (61.51%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hovanhoa committed Jan 18, 2025
1 parent 911a9ff commit 6083808
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ func longestPalindrome(s string) string {
ans := ""
for i := 0; i < len(s); i++ {
l, r := i, i
for l >= 0 && r <= len(s) - 1 && s[l] == s[r] {
for l >= 0 && r < len(s) && s[l] == s[r] {
if len(ans) < r - l + 1 {
ans = s[l:r+1]
}
Expand All @@ -11,14 +11,14 @@ func longestPalindrome(s string) string {
}

l, r = i, i + 1
for l >= 0 && r <= len(s) - 1 && s[l] == s[r] {
for l >= 0 && r < len(s) && s[l] == s[r] {
if len(ans) < r - l + 1 {
ans = s[l:r+1]
}

l, r = l - 1, r + 1
}
}
}

return ans
}

0 comments on commit 6083808

Please sign in to comment.