
😎풀이
s 순회
1-1. 현재 요소가 문자인 경우 인덱스 기록
1-2. 현재 요소가 숫자이며, 좌측에 문자가 존재했을 경우 두 문자 동시 제거 목록에 추가
s 순회
2-1. 제거 목록에 존재하지 않는 요소를 정답 문자열에 추가
- 정답 문자열 반환
function clearDigits(s: string): string {
let lastCharIdx = -1
const n = s.length
const removed = new Set()
for(let i = 0; i < n; i++) {
if(removed.has(i)) continue
if(isChar(s[i])) {
lastCharIdx = i
continue
}
if(lastCharIdx < 0) continue
removed.add(i)
removed.add(lastCharIdx)
lastCharIdx = -1
i = -1
}
let cleared = ''
for(let i = 0; i < n; i++) {
if(removed.has(i)) continue
cleared += s[i]
}
return cleared
};
function isChar(char: string) {
return isNaN(Number(char))
}