Skip to content

Commit 6fd9492

Browse files
committed
added problem 1047
1 parent 3ec4f92 commit 6fd9492

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

Diff for: problems/problem1047/remove_adj.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package problem1047
2+
3+
/*
4+
You are given a string s consisting of lowercase English letters.
5+
A duplicate removal consists of choosing two adjacent and equal letters and removing them.
6+
We repeatedly make duplicate removals on s until we no longer can.
7+
Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
8+
*/
9+
10+
func removeDuplicates(s string) string {
11+
var res = []byte(s)
12+
var cur int
13+
for i := 0; i < len(res); i++ {
14+
if cur > 0 && res[i] == res[cur-1] {
15+
cur--
16+
} else {
17+
res[cur] = res[i]
18+
cur++
19+
}
20+
}
21+
return string(res[0:cur])
22+
}

Diff for: problems/problem1047/remove_adj_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package problem1047
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Input string
12+
Expected string
13+
}
14+
15+
var Results = []Result{
16+
{"abbaca", "ca"},
17+
{"azxxzy", "ay"},
18+
}
19+
20+
func TestRemoveAdjacent(t *testing.T) {
21+
assert := assert.New(t)
22+
23+
for _, res := range Results {
24+
want := res.Expected
25+
got := removeDuplicates(res.Input)
26+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
27+
}
28+
}

Diff for: readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Each problem is in it's own directory, with test files. There are helper package
201201
| 0999 | [Available Captures for Rook](https://leetcode.com/problems/available-captures-for-rook) | [My Solution](./problems/problem0999) ||
202202
| 1009 | [Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer) | [My Solution](./problems/problem1009) ||
203203
| 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum) | [My Solution](./problems/problem1013) ||
204+
| 1047 | [Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string) | [My Solution](./problems/problem1047) ||
204205
| 1026 | [Maximum Difference Between Node and Ancestor](https://leetcode.com/problems/maximum-difference-between-node-and-ancestor) | [My Solution](./problems/problem1026) ||
205206
| 1038 | [Binary Search Tree to Greater Sum Tree](https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree) | [My Solution](./problems/problem1038) ||
206207
| 1048 | [Longest String Chain](https://leetcode.com/problems/longest-string-chain) | [My Solution](./problems/problem1048) ||

0 commit comments

Comments
 (0)