forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
33 lines (31 loc) · 844 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
private int n;
private char[] cs;
private List<String> ans = new ArrayList<>();
private boolean[] vis;
private StringBuilder t = new StringBuilder();
public String[] permutation(String S) {
cs = S.toCharArray();
n = cs.length;
Arrays.sort(cs);
vis = new boolean[n];
dfs(0);
return ans.toArray(new String[0]);
}
private void dfs(int i) {
if (i == n) {
ans.add(t.toString());
return;
}
for (int j = 0; j < n; ++j) {
if (vis[j] || (j > 0 && !vis[j - 1] && cs[j] == cs[j - 1])) {
continue;
}
vis[j] = true;
t.append(cs[j]);
dfs(i + 1);
t.deleteCharAt(t.length() - 1);
vis[j] = false;
}
}
}