This commit is contained in:
VicRen
2020-12-01 08:47:46 +08:00
parent 508e8c9c08
commit ea0f41aa12
2 changed files with 167 additions and 0 deletions

46
20201201/main.go Normal file
View File

@@ -0,0 +1,46 @@
package main
func main() {
}
func isEcho(s string) bool {
if s == "" {
return false
}
l := 0
r := len(s) - 1
for r >= l {
if s[l] != s[r] {
return false
}
l++
r--
}
return true
}
func isAlmostEcho(s string) bool {
if s == "" {
return false
}
l := 0
r := len(s) - 1
for r >= l {
if s[l] != s[r] && !isEcho(removeIndex(l, s)) && !isEcho(removeIndex(r, s)) {
return false
}
l++
r--
}
return true
}
func removeIndex(index int, s string) string {
if index >= len(s) {
return s
}
ret := []rune(s)
ret = append(ret[0:index], ret[index+1:]...)
return string(ret)
}

121
20201201/main_test.go Normal file
View File

@@ -0,0 +1,121 @@
package main
import "testing"
func Test_isEcho(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want bool
}{
{
"test1",
args{"aba"},
true,
},
{
"test2",
args{"abca"},
false,
},
{
"test3",
args{"abcba"},
true,
},
{
"test4",
args{""},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isEcho(tt.args.s); got != tt.want {
t.Errorf("isEcho() = %v, want %v", got, tt.want)
}
})
}
}
func Test_isAlmostEcho(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want bool
}{
{
"test1",
args{"aba"},
true,
},
{
"test2",
args{"abca"},
true,
},
{
"test3",
args{"abcba"},
true,
},
{
"test4",
args{""},
false,
},
{
"test5",
args{"abcbba"},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isAlmostEcho(tt.args.s); got != tt.want {
t.Errorf("isAlmostEcho() = %v, want %v", got, tt.want)
}
})
}
}
func Test_removeIndex(t *testing.T) {
type args struct {
index int
s string
}
tests := []struct {
name string
args args
want string
}{
{
"test1",
args{0, "ab"},
"b",
},
{
"test2",
args{0, ""},
"",
},
{
"test3",
args{1, "abc"},
"ac",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := removeIndex(tt.args.index, tt.args.s); got != tt.want {
t.Errorf("removeIndex() = %v, want %v", got, tt.want)
}
})
}
}