[LeetCode] 1047. Remove All Adjacent Duplicates In String

Chobby·2025년 7월 2일
1

LeetCode

목록 보기
453/480

😎풀이

  1. stack 생성
  2. 최근 입력된 요소가 중복되는지 확인
    2-1. 중복된다면 두 요소 모두 제거
    2-2. 중복되지 않는다면 현재 요소 추가
  3. 요소들을 문자열로 합쳐 반환
function removeDuplicates(s: string): string {
    const stack = []
    for(const char of s) {
        if(stack.at(-1) === char) stack.pop()
        else stack.push(char)
    }
    return stack.join('')
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글