diff --git a/problems/remove-palindromic-subsequences/remove_palindromic_subsequences.go b/problems/remove-palindromic-subsequences/remove_palindromic_subsequences.go new file mode 100644 index 000000000..4b96049a9 --- /dev/null +++ b/problems/remove-palindromic-subsequences/remove_palindromic_subsequences.go @@ -0,0 +1,13 @@ +package problem1332 + +func removePalindromeSub(s string) int { + if s == "" { + return 0 + } + for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { + if s[i] != s[j] { + return 2 + } + } + return 1 +} diff --git a/problems/remove-palindromic-subsequences/remove_palindromic_subsequences_test.go b/problems/remove-palindromic-subsequences/remove_palindromic_subsequences_test.go new file mode 100644 index 000000000..d91a2bfff --- /dev/null +++ b/problems/remove-palindromic-subsequences/remove_palindromic_subsequences_test.go @@ -0,0 +1,39 @@ +package problem1332 + +import "testing" + +type testType struct { + in string + want int +} + +func TestRemovePalindromeSub(t *testing.T) { + tests := [...]testType{ + { + in: "ababa", + want: 1, + }, + { + in: "abb", + want: 2, + }, + { + in: "baabb", + want: 2, + }, + { + in: "", + want: 0, + }, + { + in: "aaabbbaaabbb", + want: 2, + }, + } + for _, tt := range tests { + got := removePalindromeSub(tt.in) + if got != tt.want { + t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want) + } + } +}