올바른 괄호

magicdrill·2025년 1월 6일

올바른 괄호

#include<string>
#include <iostream>
#include <stack>

using namespace std;

bool solution(string s)
{
    bool answer = true;
    stack <char> st;
    int i;
    
    for(i = 0; i < s.length(); i++){
        if(st.empty()){
            st.push(s[i]);
        }
        else{
            if(s[i] == '('){
                cout << "push() ";
                st.push(s[i]);
            }
            if(s[i] == ')'){
                cout << "pop() ";
                st.pop();
            }
        }
    }

    if(st.empty()){
        cout << "\n" << st.size() << "\n";
        answer = true;
    }
    else{
        answer = false;
    }

    return answer;
}

0개의 댓글