diff --git a/20201201/main.go b/20201201/main.go new file mode 100644 index 0000000..789394e --- /dev/null +++ b/20201201/main.go @@ -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) +} diff --git a/20201201/main_test.go b/20201201/main_test.go new file mode 100644 index 0000000..51a055b --- /dev/null +++ b/20201201/main_test.go @@ -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) + } + }) + } +}