Skip to content

Commit

Permalink
cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
anjola-adeuyi committed Jul 17, 2022
1 parent 63cdb37 commit 1d1f213
Showing 1 changed file with 1 addition and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@
* @return {string}
*/
var removeDuplicates = function (s, k) {
const stack = [];
const stack = []; // [char, count];

for (const c of s) {
// Construct stack
if (stack.length !== 0 && stack[stack.length - 1][0] === c) {
stack[stack.length - 1][1]++;
} else {
stack.push([c, 1]);
}

// Remove from stack
if (stack[stack.length - 1][1] === k) {
stack.pop();
}
}

// Build output of remaining characters
return stack.reduce((res, el) => (res += el[0].repeat(el[1])), '');
};

0 comments on commit 1d1f213

Please sign in to comment.