Iterative Approach C++


#1
vector<string> Solution::generateParenthesis(int A) {
    vector <set<string>>v;
    v.push_back({""});
    v.push_back({"()"});
    for(int i=2;i<=A;i++){
        set<string>temp;
        for(int k=1;k<i;k++){
            for(auto x : v[k]){
                for(auto y : v[i-k]){
                    temp.insert(y+x);
                    temp.insert(x+y);
                }
            }
        }
        for(auto x : v[i-1]){
                temp.insert("("+x+")");
        }
        v.push_back(temp);
    }
    vector<string> ans(v[v.size()-1].begin(),v[v.size()-1].end());
    return and;
}