Posted on

Description

Submission

class Solution {
public:
    string reverseParentheses(string s) {
        stack<char> stk;
        
        for(char c: s) {
            if(c == ')') {
                string t = "";
                while(stk.top() != '(') {
                    t.push_back(stk.top());
                    stk.pop();
                }
                stk.pop();
                for(char c : t) {
                    stk.push(c);
                }
            } else {
                stk.push(c);
            }
        }
        
        string t = "";
        while(!stk.empty()) {
            if(stk.top() == '(' || stk.top() ==')') {
                stk.pop();
                continue;
            }
            t = string(1, stk.top()) + t;
            stk.pop();
        }
        return t;
    }
};

Submission 210309

class Solution {
public:
    string reverseParentheses(string s) {
        string stk = "";
        
        for(char c: s) {
            if(stk.empty()) {
                stk.push_back(c);
                continue;
            }
            
            if(c == ')') {
                string t = "";
                while(stk.back() != '(') {
                    t.push_back(stk.back());
                    stk.pop_back();
                }
                stk.pop_back();
                copy(t.begin(), t.end(), back_inserter(stk));
            } else {
                stk.push_back(c);
            }
        }
        
        return stk;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *