-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_permute.java
More file actions
78 lines (61 loc) · 2.02 KB
/
string_permute.java
File metadata and controls
78 lines (61 loc) · 2.02 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
//Not backtracking simple recursion
// print N! permutation of the characters of the string s (in order)
public static void perm1(String s) {
perm1("", s);
}
private static void perm1(String prefix, String s) {
int N = s.length();
if (N == 0) System.out.println(prefix);
else {
for (int i = 0; i < N; i++)
perm1(prefix + s.charAt(i), s.substring(0, i) + s.substring(i+1, N));
}
}
/**
* Use recursion.
1.Try each of the letters in turn as the first letter and
then find all the permutations of the remaining letters using a recursive call.
2.The base case is when the input is an empty string
the only permutation is the empty string.
* @param str
* @param index
* @param strList
*/
private void recPermutation(StringBuffer str,int index,List<String> strList) {
if(index == 0) {
strList.add(str.toString());
} else {
recPermutation(str, index -1, strList); //traverse
int currPos = str.length()-index;
for (int i = currPos+1; i < str.length(); i++) {//start swapping all other chars with current first char
swap(str,currPos, i);
recPermutation(str, index-1,strList); //for saving in list
swap(str,i, currPos);//restore back my string buffer
}
}
}
private static void swap(StringBuffer str, int pos1, int pos2){
char t1 = str.charAt(pos1);
str.setCharAt(pos1, str.charAt(pos2));
str.setCharAt(pos2, t1);
}
public static void main(String args[]) {
Map<String,Integer> opMap = new HashMap<String, Integer>();
Main m = new Main();
StringBuffer strBuffer = new StringBuffer("abc");
List<String> strList = new ArrayList<String>();
m.recPermutation(strBuffer,strBuffer.length(),strList);
System.out.println("size "+ strList.size());
for(String s:strList){
System.out.println(s);
}
for (String str : opMap.keySet()) {
System.out.print( str +" \t");
}
}
}