(프로그래머스) 괄호 변환

유지원·2022년 6월 14일
0

프로그래머스

목록 보기
64/66

문제 링크

https://programmers.co.kr/learn/courses/30/lessons/60058?language=javascript


Javascript

function reverse(str) {
  return str.slice(1, str.length - 1).split("").map((c) => (c === "(" ? ")" : "(")).join("");
}

function solution(p) {
  if (p.length < 1) return "";

  let balance = 0;
  let pivot = 0;
  do { balance += p[pivot++] === "(" ? 1 : -1 } while (balance !== 0);

  const u = p.slice(0, pivot);
  const v = solution(p.slice(pivot, p.length));

  if (u[0] === "(" && u[u.length - 1] == ")") return u + v;
  else return "(" + v + ")" + reverse(u);
}
profile
👋 https://github.com/ujw0712

0개의 댓글