Skip to content

Commit 7e2e39b

Browse files
author
Yeqi Tao
committed
Add Solution.go for 0005.Longest Palindromic Substring
1 parent 20be24e commit 7e2e39b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func longestPalindrome(s string) string {
2+
length := len(s)
3+
if length == 0 {
4+
return ""
5+
}
6+
res := make([][]bool, length)
7+
for i := 0; i < length; i++ {
8+
res[i] = make([]bool, length)
9+
}
10+
max := 1
11+
start := 0
12+
for i := 0; i < length; i++ {
13+
for j := 0; j <= i; j++ {
14+
if i - j < 2 {
15+
res[j][i] = s[j] == s[i]
16+
} else {
17+
res[j][i] = s[j] == s[i] && res[j+1][i-1]
18+
}
19+
20+
if res[j][i] && max < i - j + 1 {
21+
max = i - j + 1
22+
start = j
23+
}
24+
}
25+
}
26+
27+
return s[start:start + max]
28+
}

solution/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
├── 0005.Longest Palindromic Substring
3636
│   ├── README.md
3737
│   ├── Solution.java
38+
│   ├── Solution.go
3839
│   └── Solution.js
3940
├── 0006.ZigZag Conversion
4041
│   ├── README.md

0 commit comments

Comments
 (0)