Skip to content

Commit a24d15c

Browse files
committed
prints all permutations of string, recursively
1 parent 743ea99 commit a24d15c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package Lecture11;
2+
3+
public class StringPermutations {
4+
5+
public static void main(String[] args) {
6+
7+
String str = "abc";
8+
String result = "";
9+
10+
printPermutation(str, result);
11+
}
12+
13+
public static void printPermutation(String str, String result) {
14+
15+
if (str.length() == 0) {
16+
System.out.println(result);
17+
return;
18+
}
19+
20+
for (int i = 0; i < str.length(); i++) {
21+
char current = str.charAt(i);
22+
String before = str.substring(0, i);
23+
String after = str.substring(i + 1);
24+
String rest = before + after;
25+
26+
printPermutation(rest, result + current);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)