- Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathPermutations.java
More file actions
Latest commit
26 lines (20 loc) · 787 Bytes
/
Copy pathPermutations.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
publicclassPermutations {
publicstaticvoidmain(String[] args) {
permutations("", "abc");
}
staticvoidpermutations(Stringp, Stringup) {
// Base case: If the remaining characters to be permuted is empty, print the permutation
if (up.isEmpty()) {
System.out.println(p);
return;
}
charch = up.charAt(0);
for (inti = 0; i <= p.length(); i++) {
// Split the permutation string into two parts
Stringf = p.substring(0, i); // First part
Strings = 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));
}
}
}