[백준] - 9935 문자열 폭발 (node.js)

밀루·2025년 4월 24일
0

BOJ

목록 보기
80/82

문제링크

코드

const fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt";
const input = fs
  .readFileSync(filePath)
  .toString()
  .trim()
  .split("\n")
  .map((line) => line.replace("\r", ""));

let str = input[0].split("");
const target = input[1].split("");

let stack = [];

for (let i = 0; i < str.length; i++) {
  stack.push(str[i]);
  if (stack[stack.length - 1] === target[target.length - 1]) {
    if (
      stack.slice(stack.length - target.length, stack.length).join("") ===
      target.join("")
    ) {
      for (let i = 0; i < target.length; i++) {
        stack.pop();
      }
    }
  }
}

console.log(stack.length === 0 ? "FRULA" : stack.join(""));

스택을 사용해 풀었다. stack에 문자들을 넣으면서 target의 마지막 문자와 똑같다면 스택의 마지막부터 target의 길이만큼을 pop하는 식으로 구현했다.

profile
이밀루의 도전

0개의 댓글