문제 출처: https://programmers.co.kr/learn/courses/30/lessons/12909
Lv 2
스택하면 생각나는 전형적인 알고리즘 문제
(
면 넣고)
면 pop 한다. 하지만 stack이 empty일 때 pop 하거나 top을 알 수 없으니깐 push해서 판별하는 식으로 짰다.
#include<string>
#include <iostream>
#include <stack>
using namespace std;
bool solution(string s)
{
bool answer = true;
stack<char> st;
for(int i=0; i<s.size(); i++){
if(s[i] == '(') st.push(s[i]);
else if (s[i] == ')') {
if(st.empty()) st.push(s[i]);
else if(st.top() == '(') st.pop();
}
}
if(!st.empty()) answer = false;
return answer;
}