// 첫 번째 풀이 시간 5%
var removeDuplicates = function (s, k) {
const stack = [];
for (let i = 0; i < s.length; i++) {
const lastIndex = stack.length - 1;
let isSame = true;
stack.push(s[i]);
for (let j = 0; j < k - 1; j++) {
if (stack[lastIndex - j] !== s[i]) {
isSame = false;
break;
}
}
if (isSame) {
stack.splice(-k);
}
}
return stack.join("");
};
// 두 번째 풀이 시간 40%
var removeDuplicates = function (s, k) {
const stack = [[s[0], 1]];
for (let i = 1; i < s.length; i++) {
const curChar = s[i];
if (!stack.length) {
stack.push([curChar, 1]);
continue;
}
let [lastChar, lastSameCount] = stack[stack.length - 1];
if (lastChar === curChar) {
stack.pop();
lastSameCount++;
lastSameCount !== k && stack.push([curChar, lastSameCount]);
} else {
stack.push([curChar, 1]);
}
}
return stack.map(([char, count]) => char.repeat(count)).join("");
};
Level
LeetCode Medium
Recruitment