문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/76502
코딩테스트 연습 - 괄호 회전하기
programmers.co.kr
반시계 방향으로 문자열을 회전시키면서 올바른 괄호 문자열을 확인해봐야하는 문제였습니다.
순서대로 스택을 넣었을 때 올바른 괄호 문자열이라면 마지막에 스택이 비어있어야합니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <stack> | |
using namespace std; | |
bool isCorrect(string s) | |
{ | |
stack<char> st; | |
for (char c : s) | |
{ | |
if (!st.empty()) | |
{ | |
bool matched = false; | |
switch (st.top()) | |
{ | |
case '[': | |
if (c == ']') | |
{ | |
matched = true; | |
st.pop(); | |
} | |
break; | |
case '{': | |
if (c == '}') | |
{ | |
matched = true; | |
st.pop(); | |
} | |
break; | |
case '(': | |
if (c == ')') | |
{ | |
matched = true; | |
st.pop(); | |
} | |
break; | |
} | |
if (matched) | |
{ | |
continue; | |
} | |
} | |
st.push(c); | |
} | |
return st.empty(); | |
} | |
int solution(string s) { | |
int answer = 0; | |
for (int i = 0; i < s.length(); i++) | |
{ | |
if (isCorrect(s)) | |
{ | |
answer++; | |
} | |
char c = s.front(); | |
s = s.substr(1); | |
s += c; | |
} | |
return answer; | |
} |

개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers] 110 옮기기 (0) | 2021.10.08 |
---|---|
[Programmers] 2개 이하로 다른 비트 (2) | 2021.10.07 |
[Programmers] 약수의 개수와 덧셈 (0) | 2021.10.06 |
[Programmers] 음양 더하기 (0) | 2021.10.06 |
[Programmers 위클리 챌린지 9주차] 전력망을 둘로 나누기 (0) | 2021.10.06 |