Skip to content

Commit cc72c92

Browse files
authored
Create 1209-remove-all-adjacent-duplicates-in-string-ii.js
1 parent 3429848 commit cc72c92

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @return {string}
5+
*/
6+
const removeDuplicates = function (s, k) {
7+
const stack = [];
8+
const arr = s.split('')
9+
for(let i = 0; i < arr.length; i++) {
10+
if(i === 0 || arr[i] !== arr[i - 1]) {
11+
stack.push(1)
12+
} else {
13+
stack[stack.length - 1]++
14+
if(stack[stack.length - 1] === k) {
15+
stack.pop()
16+
arr.splice(i - k + 1, k)
17+
i -= k
18+
}
19+
}
20+
21+
}
22+
return arr.join('')
23+
};

0 commit comments

Comments
 (0)