Skip to content

Commit

Permalink
go: add 0005 Longest Palindromic Substring
Browse files Browse the repository at this point in the history
  • Loading branch information
josuebrunel committed Dec 30, 2022
1 parent 2d0e0cc commit 9ad0459
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions go/0005-Longest-Palindromic-Substring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
func longestPalindrome(s string) string {
res, resLen := "", 0

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

0 comments on commit 9ad0459

Please sign in to comment.