KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • Level 2
  • 다이나믹 프로그래밍
  • 정렬
  • Level 1
  • recursion
  • programmers
  • 문자열
  • js
  • string
  • 백준
  • Level1
  • codeup
  • LeetCode
  • Level 0
  • 수학
  • tree
  • C++
  • 제코베 JS 100제
  • 그래프
  • C

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Stack / 844번 / Backspace String Compare / JS

2023. 4. 18. 01:28

< 문제 바로가기 >

 

Backspace String Compare - LeetCode

Can you solve this real interview question? Backspace String Compare - Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character. Note that after backspacing an empty text, the tex

leetcode.com

 

< 문제 간단설명 >

문자열 s와 t가 주어진다. 문자열에 #이 있다면 이전에 입력한 문자는 지워진다. 예를들면, "ab#c" 일 경우엔 a를 입력하고 b를 입력하지만 그 다음에 #이 왔으므로 바로 직전에 입력했던 b는 지워진다. 그 후에 마지막으로 c가 입력되어서 최종적으로 "ac"가 된다.

 

위 과정을 s와 t 모두 거친 후, s와 t가 같다면 true, 아니라면 false를 반환한다.

 

/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var backspaceCompare = function(s, t) {
    let stackS = [];
    let stackT = [];

    const stacking = (string, stack) => {
        string.split('').forEach((el) => {
            if(el === '#') {
                stack.pop();
            }
            else {
                stack.push(el);
            }
        });

        return stack.join('');
    }

    return stacking(s, stackS) === stacking(t, stackT);
};
저작자표시 (새창열림)

'PS > LeetCode' 카테고리의 다른 글

LeetCode / Heap / 1046번 / Last Stone Weight / JS  (0) 2023.04.18
LeetCode / Stack / 394번 / Decode String / JS  (0) 2023.04.18
LeetCode / Binary Search Tree / 235번 / Lowest Common Ancestor of a Binary Search Tree / JS  (0) 2023.04.05
LeetCode / Binary Search Tree / 98번 / Validate Binary Search Tree / JS  (2) 2023.04.05
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Heap / 1046번 / Last Stone Weight / JS
    • LeetCode / Stack / 394번 / Decode String / JS
    • LeetCode / Binary Search Tree / 235번 / Lowest Common Ancestor of a Binary Search Tree / JS
    • LeetCode / Binary Search Tree / 98번 / Validate Binary Search Tree / JS
    KimMinJun
    KimMinJun

    티스토리툴바