diff --git a/problems/rotate-string/rotate_string.go b/problems/rotate-string/rotate_string.go index 8a0f6d230..cd845cfac 100644 --- a/problems/rotate-string/rotate_string.go +++ b/problems/rotate-string/rotate_string.go @@ -1 +1,11 @@ package rotate_string + +import "strings" + +func rotateString(A string, B string) bool { + if len(A) != len(B) { + return false + } + A = strings.Repeat(A, 2) + return strings.Contains(A, B) +} diff --git a/problems/rotate-string/rotate_string_test.go b/problems/rotate-string/rotate_string_test.go index 8a0f6d230..8fe163888 100644 --- a/problems/rotate-string/rotate_string_test.go +++ b/problems/rotate-string/rotate_string_test.go @@ -1 +1,35 @@ package rotate_string + +import "testing" + +type caseType struct { + A string + B string + expected bool +} + +func TestRotateString(t *testing.T) { + tests := [...]caseType{ + { + A: "abcde", + B: "cdeab", + expected: true, + }, + { + A: "abcde", + B: "abced", + expected: false, + }, + { + A: "abcde", + B: "abcdef", + expected: false, + }, + } + for _, tc := range tests { + output := rotateString(tc.A, tc.B) + if output != tc.expected { + t.Fatalf("input: %v %v, output: %v, expected: %v", tc.A, tc.B, output, tc.expected) + } + } +}