-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0005.go
62 lines (56 loc) · 1.32 KB
/
0005.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Source: https://leetcode.com/problems/longest-palindromic-substring
// Title: Longest Palindromic Substring
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
//
// Example 1:
//
// Input: "babad"
// Output: "bab"
// Note: "aba" is also a valid answer.
//
// Example 2:
//
// Input: "cbbd"
// Output: "bb"
//
// Constraints:
//
// 1 <= s.length <= 1000
// s consist of only digits and English letters.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
func longestPalindrome(s string) string {
n := len(s)
res := s[0:1]
// Check palindromic substring centered at i
for i := 1; i < n-1; i++ {
for j, k := i-1, i+1; j >= 0 && k < n; {
if s[j] != s[k] {
break
}
k++
if k-j > len(res) {
res = s[j:k]
}
j--
}
}
// Check palindromic substring centered at i+0.5
for i := 0; i < n-1; i++ {
for j, k := i, i+1; j >= 0 && k < n; {
if s[j] != s[k] {
break
}
k++
if k-j > len(res) {
res = s[j:k]
}
j--
}
}
return res
}