Skip to content

Commit 5784b2b

Browse files
authored
Update 1209-remove-all-adjacent-duplicates-in-string-ii.js
1 parent cc72c92 commit 5784b2b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

1209-remove-all-adjacent-duplicates-in-string-ii.js

+28
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,31 @@ const removeDuplicates = function (s, k) {
2121
}
2222
return arr.join('')
2323
};
24+
25+
// another
26+
27+
/**
28+
* @param {string} s
29+
* @param {number} k
30+
* @return {string}
31+
*/
32+
const removeDuplicates = function (s, k) {
33+
const stack = [];
34+
s = s.split('');
35+
for (let i = 0; i < s.length;) {
36+
if (i === 0 || s[i] !== s[i - 1]) {
37+
stack.push(1);
38+
i++
39+
} else {
40+
stack[stack.length - 1]++;
41+
if (stack[stack.length - 1] === k) {
42+
stack.pop();
43+
s.splice(i - k + 1, k);
44+
i = i - k + 1;
45+
} else {
46+
i++
47+
}
48+
}
49+
}
50+
return s.join('');
51+
};

0 commit comments

Comments
 (0)