String Remove Consecutive Charaa => ""; abbc => "ac"
#include <string>
#include <stack>
using namespace std;
#include <iostream>
class Solution
{
public:
string uniqString(string& str) {
bool flag = false;
stack<char> stack;
for (char c : str) {
if (stack.empty()) {
stack.push(c);
} else {
if (c == stack.top()) {
flag = true;
//if (stack.size() == 1) {
// stack.pop();
//}
} else {
if (flag) {
stack.pop();
}
flag = false;
stack.push(c);
}
//if (flag) {
// stack.pop();
//}
}
}
if (flag) {
stack.pop();
}
string result;
while (!stack.empty()) {
result = stack.top() + result;
stack.pop();
}
return result;
}
};