Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8546ffc

Browse files
author
openset
committed
Add: Remove All Adjacent Duplicates In String
1 parent 5309dc7 commit 8546ffc

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package remove_all_adjacent_duplicates_in_string
2+
3+
func removeDuplicates(S string) string {
4+
ans := make([]byte, 0, len(S))
5+
for i := 0; i < len(S); i++ {
6+
n := len(ans)
7+
if n >= 1 && ans[n-1] == S[i] {
8+
ans = ans[:n-1]
9+
} else {
10+
ans = append(ans, S[i])
11+
}
12+
}
13+
return string(ans)
14+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package remove_all_adjacent_duplicates_in_string
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input string
7+
expected string
8+
}
9+
10+
func TestRemoveDuplicates(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: "abbaca",
14+
expected: "ca",
15+
},
16+
{
17+
input: "aaabaca",
18+
expected: "abaca",
19+
},
20+
}
21+
for _, tc := range tests {
22+
output := removeDuplicates(tc.input)
23+
if output != tc.expected {
24+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
25+
}
26+
}
27+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /