-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathlongest-palindromic-substring.go
More file actions
74 lines (61 loc) · 1.62 KB
/
longest-palindromic-substring.go
File metadata and controls
74 lines (61 loc) · 1.62 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
package main
func longestPalindrome(s string) string {
dp := [][]bool{}
for _ = range s {
dp = append(dp, make([]bool, len(s)))
}
maxPalindromeLen, maxPalindromLeft, maxPalindromeRight := 0, 0, -1
for left := len(s) - 1; left >= 0; left-- {
for right := left; right < len(s); right++ {
if left == right {
dp[left][right] = true
} else if left+1 == right {
dp[left][right] = s[left] == s[right]
} else {
dp[left][right] = dp[left+1][right-1] && s[left] == s[right]
}
if dp[left][right] && right-left+1 > maxPalindromeLen {
maxPalindromeLen = right - left + 1
maxPalindromLeft, maxPalindromeRight = left, right
}
}
}
return s[maxPalindromLeft : maxPalindromeRight+1]
}
func longestPalindromeTopDown(s string) string {
var isPalindrome func(start, end int) bool
isPalindrome = func(start, end int) bool {
if start >= end {
return true
}
if s[start] == s[end] {
return isPalindrome(start+1, end-1)
} else {
return false
}
}
maxPalindromeLen, maxPalindromLeft, maxPalindromeRight := 0, 0, 0
cache := []map[int]bool{}
for _ = range s {
cache = append(cache, map[int]bool{})
}
for left := range s {
for right := left; right < len(s); right++ {
palindrome := false
if val, ok := cache[left][right]; ok {
palindrome = val
} else {
palindrome = isPalindrome(left, right)
cache[left][right] = palindrome
}
if palindrome && right-left+1 > maxPalindromeLen {
maxPalindromeLen = right - left + 1
maxPalindromLeft, maxPalindromeRight = left, right
}
}
}
if len(s) == 0 {
return ""
}
return s[maxPalindromLeft : maxPalindromeRight+1]
}