-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram70.java
More file actions
23 lines (19 loc) · 775 Bytes
/
Program70.java
File metadata and controls
23 lines (19 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Program70 {
//Print all permutations of a string
public static void printPerm(String str, String permutation) {
if (str.length() == 0) {
System.out.println(permutation);
return;
}
for (int i = 0; i < str.length(); i++) {
char currChar = str.charAt(i);
//"abc" -> "ab"
String newString = str.substring(0, i) + str.substring(i + 1); //remove the current character from the string
printPerm(newString, permutation + currChar); //add the current character to permutation
}
}
public static void main(String[] args) {
String str = "abc";
printPerm(str, ""); //time complexity : O(n*n!) -> O(n!)
}
}