KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (487)
    • 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 (432)
      • 백준 (187)
      • Programmers (105)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

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

공지사항

인기 글

태그

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

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun
PS/LeetCode

LeetCode / Stack / 394번 / Decode String / JS

PS/LeetCode

LeetCode / Stack / 394번 / Decode String / JS

2023. 4. 18. 01:33

< 문제 바로가기 >

 

Decode String - LeetCode

Can you solve this real interview question? Decode String - Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is g

leetcode.com

 

< 문제 간단설명 >

[] 괄호 쌍 안에 있는 문자열을 바로 앞에 온 숫자만큼 반복한다. 예를 들어 "3[a]2[bc]" 일 경우엔, "aaabcbc" 가 된다.

 

/**
 * @param {string} s
 * @return {string}
 */
var decodeString = function(s) {
    let stack = [];
    let curStr = '';
    let curCnt = '';

    s.split('').forEach((el) => {
        if(el === '[') {
            stack.push(curCnt);
            stack.push(curStr);
            curStr = '';
            curCnt = '';
        }
        else if(el === ']') {
            let prevStr = stack.pop();
            let prevCnt = stack.pop();
            curStr = prevStr + curStr.repeat(Number(prevCnt));
        }
        else if('0' <= el && el <= '9') {
            curCnt += el;
        }
        else {
            curStr += el;
        }
    });

    return curStr;
};
저작자표시 (새창열림)

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

LeetCode / Heap / 692번 / Top K Frequent Words / JS  (0) 2023.04.18
LeetCode / Heap / 1046번 / Last Stone Weight / JS  (0) 2023.04.18
LeetCode / Stack / 844번 / Backspace String Compare / JS  (0) 2023.04.18
LeetCode / Binary Search Tree / 235번 / Lowest Common Ancestor of a Binary Search Tree / JS  (0) 2023.04.05
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Heap / 692번 / Top K Frequent Words / JS
    • LeetCode / Heap / 1046번 / Last Stone Weight / JS
    • LeetCode / Stack / 844번 / Backspace String Compare / JS
    • LeetCode / Binary Search Tree / 235번 / Lowest Common Ancestor of a Binary Search Tree / JS
    KimMinJun
    KimMinJun

    티스토리툴바

    단축키

    내 블로그

    내 블로그 - 관리자 홈 전환
    Q
    Q
    새 글 쓰기
    W
    W

    블로그 게시글

    글 수정 (권한 있는 경우)
    E
    E
    댓글 영역으로 이동
    C
    C

    모든 영역

    이 페이지의 URL 복사
    S
    S
    맨 위로 이동
    T
    T
    티스토리 홈 이동
    H
    H
    단축키 안내
    Shift + /
    ⇧ + /

    * 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.