#include<string>
#include <iostream>
using namespace std;
bool solution(string str)
{
int cnt = 0;
for (const auto s : str) {
if (cnt < 0)
return false;
if (s == '(')
++cnt;
else if (s == ')')
--cnt;
}
// [실행] 버튼을 누르면 출력 값을 볼 수 있습니다.
cout << "Hello Cpp" << endl;
return (cnt == 0);
}
다른 사람 풀이
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool solution(string s)
{
int T;
vector<int> stack;
for(int j=0;j<s.size();j++){
if(stack.empty() && s.at(j)==')'){
stack.push_back(1);
break;
}
else {
if(s.at(j)=='(') stack.push_back(1);
else stack.pop_back();
}
}
// [실행] 버튼을 누르면 출력 값을 볼 수 있습니다.
return stack.empty();
}