-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathPermuteString.java
89 lines (84 loc) · 3.82 KB
/
PermuteString.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.thealgorithms.strings;
import java.util.HashSet;
import java.util.Set;
/**
* This class provides methods for generating all permutations of a given string using a backtracking algorithm.
* <p>
* The algorithm works as follows:
* <ol>
* <li>Fix a character in the current position and swap it with each of the remaining characters.
* For example, for the string "ABC":
* <ul>
* <li>Fix 'A' at the first position: permutations are "ABC", "BAC", "CBA" (obtained by swapping 'A' with 'B' and 'C' respectively).</li>
* </ul>
* </li>
* <li>Repeat the process for the next character.
* For instance, after fixing 'B' in the second position:
* <ul>
* <li>For "BAC", the permutations include "BAC" and "BCA" (after swapping 'A' and 'C').</li>
* </ul>
* </li>
* <li>After generating permutations for the current position, backtrack by swapping the characters back to their original positions to restore the state.
* For example, after generating permutations for "ABC", swap back to restore "BAC" and continue with further permutations.</li>
* <li>Repeat the process for all characters to get all possible permutations.</li>
* </ol>
* </p>
*/
public final class PermuteString {
private PermuteString() {
}
/**
* Generates all possible permutations of the given string.
*
* <p>This method returns a set containing all unique permutations of the input string. It leverages
* a recursive helper method to generate these permutations.
*
* @param str The input string for which permutations are to be generated.
* If the string is null or empty, the result will be an empty set.
* @return A {@link Set} of strings containing all unique permutations of the input string.
* If the input string has duplicate characters, the set will ensure that only unique permutations
* are returned.
*/
public static Set<String> getPermutations(String str) {
Set<String> permutations = new HashSet<>();
generatePermutations(str, 0, str.length(), permutations);
return permutations;
}
/**
* Generates all permutations of the given string and collects them into a set.
*
* @param str the string to permute
* @param start the starting index for the current permutation
* @param end the end index (length of the string)
* @param permutations the set to collect all unique permutations
*/
private static void generatePermutations(String str, int start, int end, Set<String> permutations) {
if (start == end - 1) {
permutations.add(str);
} else {
for (int currentIndex = start; currentIndex < end; currentIndex++) {
// Swap the current character with the character at the start index
str = swapCharacters(str, start, currentIndex);
// Recursively generate permutations for the remaining characters
generatePermutations(str, start + 1, end, permutations);
// Backtrack: swap the characters back to their original positions
str = swapCharacters(str, start, currentIndex);
}
}
}
/**
* Swaps the characters at the specified positions in the given string.
*
* @param str the string in which characters will be swapped
* @param i the position of the first character to swap
* @param j the position of the second character to swap
* @return a new string with the characters at positions i and j swapped
*/
private static String swapCharacters(String str, int i, int j) {
char[] chars = str.toCharArray();
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
return new String(chars);
}
}