-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathPermutations.java
More file actions
26 lines (20 loc) · 787 Bytes
/
Permutations.java
File metadata and controls
26 lines (20 loc) · 787 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
public class Permutations {
public static void main(String[] args) {
permutations("", "abc");
}
static void permutations(String p, String up) {
// Base case: If the remaining characters to be permuted is empty, print the permutation
if (up.isEmpty()) {
System.out.println(p);
return;
}
char ch = up.charAt(0);
for (int i = 0; i <= p.length(); i++) {
// Split the permutation string into two parts
String f = p.substring(0, i); // First part
String s = p.substring(i, p.length()); // Second part
// Recursive call: Generate permutations by inserting the current character at each possible position
permutations(f + ch + s, up.substring(1));
}
}
}