-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathh_permutation.java
More file actions
32 lines (28 loc) · 961 Bytes
/
Copy pathh_permutation.java
File metadata and controls
32 lines (28 loc) · 961 Bytes
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
import java.util.ArrayList;
public class h_permutation {
static ArrayList<String> perm(String str) {
if(str.length() == 0) {
ArrayList<String> list = new ArrayList<>();
list.add("");
return list;
}
// currentChar - a,b,c
char currentChar = str.charAt(0);
String remaningString = str.substring(1);
ArrayList<String> temp = perm(remaningString);
ArrayList<String> result = new ArrayList<>();
for(String s : temp) {
for(int i = 0; i <= s.length(); i++) {
StringBuffer sb = new StringBuffer(s);
sb.insert(i, currentChar);
result.add(sb.toString());
}
}
return result;
}
public static void main(String[] args) {
String str = "abc";
ArrayList<String> result = perm(str);
System.out.println(result);
}
}