#include <iostream>
#include<string>
using namespace std;
int solution(string s)
{
int answer = -1;
if(s.size() <= 1) return 0;
string checkS = "_";
while(checkS!="")
{
checkS="";
bool isTrue = false;
int idx = -1;
for(int i = 0 ; i<s.size()-1;i++)
{
if(s[i] == s[i+1] && !isTrue)
{
isTrue = true;
checkS += s.substr(i+2,s.size());
break;
}
else
{
checkS += s[i];
}
}
if(!isTrue) return 0;
s=checkS;
}
return 1;
}
ㅜ 보니까 효율성때문에 stack으로 풀어줘야한다
#include <stack>
#include <string>
using namespace std;
int solution(string s)
{
stack<char> st;
for (int i = 0; i < s.size(); ++i)
{
if (st.empty())
st.push(s[i]);
else if (st.top() == s[i])
st.pop();
else
st.push(s[i]);
}
return st.empty();
}
짝짓는 문제는 stack을 사용하면 효율적일 것 같다.
댓글