링크 : https://school.programmers.co.kr/learn/courses/30/lessons/76502
스텍을 사용해서 풀었다.
#include <string>
#include <vector>
#include <stack>
#include <iostream>
using namespace std;
bool check(string s){
stack<char> st;
for(int i = 0; i < s.size(); i++){
if(s[i] == '(' || s[i] == '[' || s[i] == '{') st.push(s[i]);
if(s[i] == ')'){
if(!st.empty() && st.top() == '(') st.pop();
else st.push(s[i]);
}
if(s[i] == ']'){
if(!st.empty() && st.top() == '[') st.pop();
else st.push(s[i]);
}
if(s[i] == '}'){
if(!st.empty() && st.top() == '{') st.pop();
else st.push(s[i]);
}
}
if(st.empty()) return true;
else return false;
}
int solution(string s) {
int answer = 0;
for(int i = 0; i < s.size(); i ++){
char tmp = s.front();
s.erase(s.begin());
s.push_back(tmp);
if(check(s)) answer++;
}
return answer;
}