-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation.java
More file actions
41 lines (39 loc) · 887 Bytes
/
permutation.java
File metadata and controls
41 lines (39 loc) · 887 Bytes
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
package algo;
/**
* This program is for printing all the permutations of a string
* using recursion
* n characters string has n! combinations
* It also checks if any combination presents in another string
* @author Azmain
*
*/
public class permutation {
public static void main(String[] args) {
permute("","use");
}
/**
* fix is which is set
* rest is which need to be permuted
* @param fix
* @param rest
*/
public static void permute(String fix,String rest) {
if(rest.isEmpty()) {
System.out.println(fix);
if("quest".contains(fix)) {
System.out.println("True");
}
}
/**
* Below recursive call does -
* a(bc)->ab(c)->ac(b)
* b(ac)->ba(c)->bc(a)
* c(ab)->ca(b)->cb(a)
*/
else {
for(int i = 0; i < rest.length(); i++) {
permute(fix+rest.charAt(i),rest.substring(0, i)+rest.substring(i+1, rest.length()));
}
}
}
}