Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package remove_all_adjacent_duplicates_in_string

func removeDuplicates(S string) string {
ans := make([]byte, 0, len(S))
for i := 0; i < len(S); i++ {
n := len(ans)
if n >= 1 && ans[n-1] == S[i] {
ans = ans[:n-1]
} else {
ans = append(ans, S[i])
}
}
return string(ans)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package remove_all_adjacent_duplicates_in_string

import "testing"

type caseType struct {
input string
expected string
}

func TestRemoveDuplicates(t *testing.T) {
tests := [...]caseType{
{
input: "abbaca",
expected: "ca",
},
{
input: "aaabaca",
expected: "abaca",
},
}
for _, tc := range tests {
output := removeDuplicates(tc.input)
if output != tc.expected {
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
}
}
}