mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-25 05:22:39 +08:00
@ -7,13 +7,13 @@ Backtracking algorithm used in the program:-
|
||||
Like in ABC, in the first iteration three strings are formed: ABC, BAC, and CBA by swapping A with
|
||||
A, B and C respectively.
|
||||
>>Repeat step 1 for the rest of the characters like fixing second character B and so on.
|
||||
>>Now swap again to go back to the previous position. E.g., from ABC, we formed ABC by fixing B again,
|
||||
and we backtrack to the previous position and swap B with C. So, now we got ABC and ACB.
|
||||
>>Now swap again to go back to the previous position. E.g., from ABC, we formed ABC by fixing B
|
||||
again, and we backtrack to the previous position and swap B with C. So, now we got ABC and ACB.
|
||||
>>Repeat these steps for BAC and CBA, to get all the permutations.
|
||||
*/
|
||||
public class PermuteString {
|
||||
|
||||
//Function for swapping the characters at position I with character at position j
|
||||
// Function for swapping the characters at position I with character at position j
|
||||
public static String swapString(String a, int i, int j) {
|
||||
char[] b = a.toCharArray();
|
||||
char ch;
|
||||
@ -30,18 +30,18 @@ public class PermuteString {
|
||||
generatePermutation(str, 0, len);
|
||||
}
|
||||
|
||||
//Function for generating different permutations of the string
|
||||
// Function for generating different permutations of the string
|
||||
public static void generatePermutation(String str, int start, int end) {
|
||||
//Prints the permutations
|
||||
// Prints the permutations
|
||||
if (start == end - 1) {
|
||||
System.out.println(str);
|
||||
} else {
|
||||
for (int i = start; i < end; i++) {
|
||||
//Swapping the string by fixing a character
|
||||
// Swapping the string by fixing a character
|
||||
str = swapString(str, start, i);
|
||||
//Recursively calling function generatePermutation() for rest of the characters
|
||||
// Recursively calling function generatePermutation() for rest of the characters
|
||||
generatePermutation(str, start + 1, end);
|
||||
//Backtracking and swapping the characters again.
|
||||
// Backtracking and swapping the characters again.
|
||||
str = swapString(str, start, i);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user