-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbacktracking.java
More file actions
22 lines (19 loc) · 902 Bytes
/
backtracking.java
File metadata and controls
22 lines (19 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class backtracking {
public static void printPermutation(String str, int idx, String perm){//int indx is for placing the string xter on any position
//time complexity=O(n*n!)
if (str.length() == 0) {//to know if all the charaters are removed
System.out.println(perm);
return;
}
for (int i = 0; i < str.length(); i++) {//for traversing the string n which char to put in the current position
char currChar = str.charAt(i);// current character
//for removing the current character n take a new substring which is i+1
String newStr = str.substring(0, i) + str.substring(i + 1);
printPermutation(newStr, idx + 1, perm + currChar);
}
}
public static void main(String args[]) {
String str = "abc";
printPermutation(str, 0, "");
}
}