
https://school.programmers.co.kr/learn/courses/30/lessons/76502
const numbering = {
"[" : 0,
"]" : 1,
"{" : 2,
"}" : 3,
"(" : 4,
")" : 5
}
function solution(s) {
let answer = 0;
for( let i = 0; i < s.length; i++){
s = s.slice(1) + s[0] // 왼쪽으로 한번씩 밀기
const stack = []
for(let j = 0; j < s.length; j ++) {
// 닫힌 괄호인지, 열린 괄호인지를 판단( 열림 : 짝수, 닫힘 : 홀수 )
if( numbering[s[j]] %2 === 0 ) {
stack.push(numbering[s[j]])
} else {
// 닫힌괄호라면 Stack에 그 짝이 있는지 체크
if(stack.includes(numbering[s[j]] - 1)){
const last = stack[stack.length - 1]
if(last === numbering[s[j]] -1){
stack.splice(stack.length - 1,1)
}
} else {
// 열린 괄호가 없다면 반복문 중단
break;
}
}
// 가장 마지막을 체크하면서, 모든 괄호의 짝이 맞을 때
if( j === s.length - 1 ){
if(stack.length === 0) {
answer++
}
}
}
}
return answer
}
'알고리즘 공부장' 카테고리의 다른 글
09. [Lv2] 프린터 (0) | 2022.12.18 |
---|---|
08. [Lv2] 스킬트리 (0) | 2022.12.09 |
05. [Lv1] 수박수박수박수박수박수? (0) | 2022.11.11 |
04. [Lv1] 나머지가 1이 되는 수 찾기 (0) | 2022.11.09 |
03. [Lv1] 정수 내림차순으로 배치하기 (0) | 2022.11.09 |