Finding String Permutations in Java: A Guide to Recursive Methods
🚀 What Will You Learn in This Guide?
In this guide, you will learn step by step how to find all possible permutations of a string in Java.
The recursive approach prevents repetition with the Set structure and
You will see in practice how to produce high-performance results.
🧠 Technical Summary
Topic: Generating String permutations in Java
Purpose: To find all possible sequences (permutations) that can be created from the characters of a string
Solved Problem: Finding different sequences in combination, anagram or sorting algorithms
Basic Steps:
- Get the first character of the string.
- Recursively calculate permutations of the remaining characters.
- Insert the first character into all positions of each subpermutation.
- Avoid duplication by collecting all results in
Set.
🧩 Java Permutation Algorithm Logic
Example: String = "ABC"
1️⃣ Reserve first character → "A"
2️⃣ Find permutations for the remaining "BC" → "BC", "CB"
3️⃣ Place the character "A" in each position of these results:
BC → ABC, BAC, BCA CB → ACB, CAB, CBA
pgsql
Result: [ABC, ACB, BAC, BCA, CAB, CBA]
🧠 This method generates all combinations recursively.
💻 Finding String Permutations in Java
import java.util.HashSet;
import java.util.Set;
public class StringPermutasyon {
// Verilen dizenin tüm permütasyonlarını döndürür.
public static Set<String> permutationFinder(String str) {
Set<String> perm = new HashSet<>();
if (str == null) return null;
if (str.length() == 0) {
perm.add("");
return perm;
}
char ilkKarakter = str.charAt(0);
String kalan = str.substring(1);
Set<String> altPermutasyonlar = permutationFinder(kalan);
for (String kelime : altPermutasyonlar) {
for (int i = 0; i <= kelime.length(); i++) {
perm.add(charInsert(kelime, ilkKarakter, i));
}
}
return perm;
}
// Bir karakteri dizenin belirtilen pozisyonuna ekler.
public static String charInsert(String str, char c, int j) {
String bas = str.substring(0, j);
String son = str.substring(j);
return bas + c + son;
}
// Test için ana metot
public static void main(String[] args) {
String d1 = "AAB";
String d2 = "KLM";
System.out.println("\nPermütasyonlar " + d1 + " için: \n" + permutationFinder(d1));
System.out.println("\nPermütasyonlar " + d2 + " için: \n" + permutationFinder(d2));
}
}
🧠 This code produces unique permutations by filtering repeated letters with the help of Set.
📤 Sample Output
Permütasyonlar AAB için:
[AAB, ABA, BAA]
Permütasyonlar KLM için:
[KLM, KML, LKM, LMK, MKL, MLK]
💡 HashSet automatically eliminates permutations that produce the same result (e.g. repeats in “AAB”).
🔍 Steps of the Algorithm (PivotTable)
| 🧩 Step | ⚙️ Transaction | 💡 Description |
|---|---|---|
| 1️⃣ | First character is taken | For example "JAVA" → "J" + "AVA" |
| 2️⃣ | The remaining characters are processed recursively | "AVA" → "AVA", "AAV", "VAA" |
| 3️⃣ | The first character is added to each position | "J" → "JAVA", "AJVA", "AVAJ" |
| 4️⃣ | Results collected at Set | Duplicate results are automatically eliminated. |
⏱️ Time Complexity
The number of permutations is n! It grows up to (factorial). That is, as the number of characters increases, the processing time increases dramatically.
| ⚙️ Input Length | 🔢 Number of Permutations | ⏱️ Time Complexity |
|---|---|---|
| 3 | 6 | O(n!) |
| 4 | 24 | O(n!) |
| 5 | 120 | O(n!) |
| 6 | 720 | O(n!) |
💬 Frequently Asked Questions (FAQ)
- Why do we use HashSet?
Because if there are identical characters (like AAB), the Set structure automatically eliminates duplicate results.
- Why is Set preferred over ArrayList?
While ArrayList stores all results, Set filters out unique values. This is especially important with repetitive characters.
- Why is the time complexity O(n!)?
Each character is added to different positions, which increases the number of combinations by a factorial ratio.
- What happens when the string is empty?
The function returns a single empty string ([""]) and does not throw an error.
- Is there any faster method?
There is no ready-made permutation function in Java. However, for big data, iterative algorithms or parallel processing (multithreading) can be used.
🏁 Conclusion
In this guide, you learned step by step how to find all permutations of a string in Java. The recursive structure is a perfect example of how to both understand the logic and stand out in interviews. Producing unique results with the use of sets provides a professional approach.
☁️ You can try your own algorithms with performance by testing them in the Java working environment on the GenixNode platform!

