-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermute.java
56 lines (35 loc) · 1.6 KB
/
Permute.java
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.freestyle;
public class Permute {
public static void main(String[] args) {
System.out.println("My code: ");
Permutations("abcd", "", 4);
System.out.println("\nNow here we have Ma'am's code: ");
PrintPermutations("abcd", "", 0);
}
//My Code:
public static void Permutations(String s, String str, int length){
if(str.length() == length){
System.out.println(str);
return;
}
for (int i = 0; i < s.length(); i++) {
Permutations(s.substring(0,i) + s.substring(i+1), str + s.charAt(i), length);
}
}
//both my code and Ma'am's code have a similar approach except for the fact that she is using
//the length of the initial string (here str) as a counter, so as soon as the length of the initial string when
//passed by recursion becomes 0, the program prints the permutation (here perm) and returns to previous call
//Meanwhile, in my program, I am using the length of the permutation as a counter, so as soon as the
//permutation (here str), becomes the size of the initial string, I print the permutation (str) and
//return back to the previous call
//Ma'am Code:
public static void PrintPermutations(String str, String perm, int index){
if(str.length() == 0){
System.out.println(perm);
return;
}
for (int i = 0; i < str.length(); i++) {
PrintPermutations(str.substring(0,i) + str.substring(i+1), perm + str.charAt(i), index + 1);
}
}
}