无重复字符串的排列组合。编写一种方法,计算某字符串的所有排列组合,字符串每个字符均不相同。
示例1:
输入:S = "qwe"
输出:["qwe", "qew", "wqe", "weq", "ewq", "eqw"]
示例2:
输入:S = "ab"
输出:["ab", "ba"]
提示:
- 字符都是英文字母。
- 字符串长度在[1, 9]之间。
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String[] permutation(String S) {
if (S == null || S.length() == 0) {
return new String[0];
}
List<String> result = new ArrayList<>();
doPermute(result, "", S);
return result.toArray(new String[result.size()]);
}
private void doPermute(List<String> result, String curS, String S) {
if (curS.length() == S.length()) {
result.add(new String(curS));
return;
}
for (char ch : S.toCharArray()) {
if (curS.indexOf(ch) == -1) {
doPermute(result, curS + ch, S);
}
}
}
}
//leetcode submit region end(Prohibit modification and deletion)