Posted on

Description

Submission

class Solution {
    set<string> rets;
    vector<int> taken;
    int n;
    string s;

    void backtrack(int pos, string& cur) {
        if(pos == n) {
            rets.insert(cur);
            return;
        }

        for(int i = 0; i < n; ++i) {
            if(!taken[i]) {
                taken[i] = 1;
                cur.push_back(s[i]);
                backtrack(pos+1, cur);
                cur.pop_back();
                taken[i] = 0;
            }
        }
    }
public:
    vector<string> permutation(string s) {
        n = s.length();
        taken.resize(n, 0);
        this->s = s;
        string cur;

        backtrack(0, cur);

        return {rets.begin(), rets.end()};
    }
};
class Solution {
public:
    vector<string> permutation(string s) {
        vector<string> rets;
        sort(s.begin(), s.end());

        do {
            rets.push_back(s);
        } while(next_permutation(s.begin(), s.end()));

        return rets;
    }
};

Leave a Reply

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