-
Notifications
You must be signed in to change notification settings - Fork 1
/
bp.go
85 lines (80 loc) · 1.41 KB
/
bp.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package bp
func breakPalindrome(s string) string {
return iteratorV2(s)
}
// bruteForce time complexity O(N^2)
func bruteForce(s string) string {
n := len(s)
var res string
if n == 1 {
return res
}
for i := range s {
for c := 'a'; c <= 'z'; c++ {
sb := []byte(s)
if sb[i] != byte(c) {
sb[i] = byte(c)
tmpsb := string(sb)
if !isPalindrome(tmpsb) {
if res == "" {
res = tmpsb
} else {
if tmpsb < res {
res = tmpsb
}
}
}
}
}
}
return res
}
func isPalindrome(s string) bool {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
if s[i] != s[j] {
return false
}
}
return true
}
// iterator time complexity is O(N)
func iterator(s string) string {
n := len(s)
if n <= 1 {
return ""
}
sb := []byte(s)
for i := 0; i < n; i++ {
j := n - 1 - i
if i == j {
// n is odd, skip this character
// for example aacaa, if we change c to a, this is still a palindrome
continue
}
if sb[i] != byte('a') {
sb[i] = byte('a')
return string(sb)
}
}
// all 'a's
sb[n-1] = byte('b')
return string(sb)
}
// iteratorV2 time complexity is O(N)
func iteratorV2(s string) string {
n := len(s)
if n <= 1 {
return ""
}
sb := []byte(s)
// s is a palindrome, we just check half the string
for i := 0; i < n/2; i++ {
if sb[i] != byte('a') {
sb[i] = byte('a')
return string(sb)
}
}
// all 'a's
sb[n-1] = byte('b')
return string(sb)
}