개발의변화
프로그래머스 LV2 괄호 회전하기 본문
반응형
문제
https://school.programmers.co.kr/learn/courses/30/lessons/76502
답
const pair = { '}': '{', ']': '[', ')': '(' }
function solution(s) {
const arr = s.split('')
let result = 0
const isValid = arr => {
const stack = []
for (let i = 0; i < arr.length; i++) {
const c = arr[i]
if (pair[c] === undefined) stack.push(c)
else {
if (stack[stack.length - 1] !== pair[c]) return false
stack.pop()
}
}
if (stack.length) return false
return true
}
for (let i = 0; i < s.length; i++) {
if (isValid(arr)) result++
arr.push(arr.shift())
}
반응형
'알고리즘' 카테고리의 다른 글
프로그래머스 LV2 파일명 정렬(카카오) (0) | 2023.04.06 |
---|---|
프로그래머스 LV2 피보나치 수 (0) | 2023.04.02 |
프로그래머스 LV2 방문길이 (0) | 2023.03.28 |
프로그래머스 LV2 땅따먹기 (0) | 2023.03.24 |
프로그래머스 LV2 스킬트리 (0) | 2023.03.24 |