面试题38

输入一个字符串,打印出该字符串中字符的所有排列。

你可以以任意顺序返回这个字符串数组,但里面不能有重复元素。

示例:

输入:s = "abc"
输出:["abc","acb","bac","bca","cab","cba"]

限制:

  • 1 <= s 的长度 <= 8

Solutions

  1. backtrack with counter

class Solution {
public:
    vector<string> res;
    vector<int> m;
    string curs;

    void dfs(string & s) {
        if (curs.size() == s.size())
            res.push_back(curs);
        else {
            for (int c = 0; c < 26; c++) {
                if (m[c] <= 0)
                    continue;
                m[c]--;
                curs.push_back('a' + c);
                dfs(s);
                curs.pop_back();
                m[c]++;
            }
        }
    }
    vector<string> permutation(string s) {
        m =  vector<int>(26);
        for (auto & c : s)
            m[c - 'a']++;
        dfs(s);

        return res;
    }
};
  1. backtrack with swap

  2. In each recursive call, using numbers(characters) with the same value are forbidden.

  3. Since swap could break the continuousness of two numbers, additional steps are required for checking duplicates.

  • Or another version based on sorted string, we always choose to swap the current character with the inserted character(first) and do not swap back.

  • For example:

    • aabbcc, when the insertion position is 0, there are 3 possible strings.

    • aabbcc

    • baabcc

    • caabbc

    • The ordering of each pair of character doesn't change after the swap operation which is different from that in the previous solution.

Last updated

Was this helpful?