forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1216.go
37 lines (33 loc) · 776 Bytes
/
1216.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
func isValidPalindrome(s string, k int) bool {
return len(s) - lcs(s, Reverse(s)) <= k
}
func lcs(text1 string, text2 string) int {
m, n := len(text1), len(text2)
l := make([][]int, m + 1)
for i := 0; i <= m; i++ {
l[i] = make([]int, n + 1)
}
for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {
if text1[i-1] == text2[j-1] {
l[i][j] = l[i-1][j-1] + 1
} else {
l[i][j] = max(l[i][j-1], l[i-1][j])
}
}
}
return l[m][n]
}
func Reverse(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}