Skip to content

Commit c85b585

Browse files
committed
print string permu
1 parent a25e2fd commit c85b585

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.ArrayList;
2+
public class Recursion2_PrintPermutation{
3+
// private static String FindPermutations(String input, String output){
4+
5+
// String ans = "";
6+
7+
// if (input.length()==0){
8+
// System.out.println(output);
9+
// return null;
10+
// }
11+
12+
// for(int i = 0; i<input.length(); i++){
13+
// ans = FindPermutations(input.substring(0,i) + input.substring(i+1), output + input.charAt(i));
14+
// }
15+
16+
// return ans;
17+
18+
// }
19+
20+
21+
public static ArrayList<String> FindPermutations(String str) {
22+
23+
// String output = "";
24+
// ArrayList<String> jawaab = FindPermutations(STR, output);
25+
// return jawaab;
26+
27+
28+
// // Write your code here!
29+
30+
// }
31+
32+
if (str.length() == 0) {
33+
ArrayList<String> empty = new ArrayList<>();
34+
empty.add("");
35+
return empty;
36+
}
37+
38+
char ch = str.charAt(0);
39+
String subStr = str.substring(1);
40+
41+
ArrayList<String> lastCombination = FindPermutations(subStr);
42+
ArrayList<String> newCombination = new ArrayList<>();
43+
for (String val : lastCombination) {
44+
for (int i = 0; i <= val.length(); i++) {
45+
newCombination.add(val.substring(0, i) + ch + val.substring(i));
46+
}
47+
}
48+
return newCombination;
49+
}
50+
51+
}

0 commit comments

Comments
 (0)