[Programmers] 괄호 변환
Date:
[Programmers] 괄호 변환
Problem URL : 괄호 변환
#include <string>
#include <vector>
using namespace std;
string flip(string u) {
string ret = "";
for (int i = 0; i < u.size(); i++) {
if (u[i] == '(') {
ret += ")";
} else {
ret += "(";
}
}
return ret;
}
int count(char bracket) {
if (bracket == '(') {
return 1;
}
return -1;
}
string dfs(string p) {
if (p == "") {
return p;
}
int sum = 0;
sum += count(p[0]);
int idx = 1;
bool proper = true;
while (sum != 0) {
if (sum < 0) {
proper = false;
}
sum += count(p[idx]);
idx++;
}
string u = p.substr(0, idx);
string v = p.substr(idx);
if (proper) {
return u + dfs(v);
} else {
return "(" + dfs(v) + ")" + flip(u.substr(1,u.size() - 2));
}
}
string solution(string p) {
return dfs(p);
}
댓글