有重复字符串的排列组合。编写一种方法,计算某字符串的所有排列组合。

示例1:

输入:S = “qqe”
输出:[“eqq”,“qeq”,“qqe”]
示例2:

输入:S = “ab”
输出:[“ab”, “ba”]
提示:

字符都是英文字母。
字符串长度在[1, 9]之间。

java代码:

class Solution {
    boolean[] vis;
    public String[] permutation(String S) {
        int length = S.length();
        List<String> res_l = new ArrayList<String>();
        List<Character> char_l = new ArrayList<Character>();
        List<Character> path = new ArrayList<Character>();
        vis = new boolean[length];
        for(int i = 0; i < length; i++){
            char_l.add(S.charAt(i));
        }
        // 排序
        Collections.sort(char_l);

        // 数组,结果,起点,路径
        backtrack(res_l, path, char_l, 0 , length);

        // 结果的转化与输出
        String [] res = new String[res_l.size()];
        int index = 0;
        for (String ch :res_l){
            res[index++] = ch;
        }

        return res;
    }

    public void backtrack(List<String> res_l, List<Character> path, List<Character> char_l, int first, int length) {
        if (first == length) {	// 到底了,存入
            // 转化 List<Character> 转 String
            StringBuilder sb = new StringBuilder();
            for (Character ch :path){
                sb.append(ch);
            }
            String str = sb.toString();
            res_l.add(str);
            return;
        }
        
        for (int i = 0; i < length; ++i) {
            // 去重
            if (vis[i] || (i > 0 && char_l.get(i) == char_l.get(i - 1) && !vis[i - 1])) {
                continue;
            }
            // 加入
            path.add(char_l.get(i));
            vis[i] = true;

            // 递归
            backtrack(res_l, path, char_l,  first + 1, length);
            
            // 回溯,取出
            vis[i] = false;
            path.remove(first);
        }
    }


}