Specific element permutation within an array of characters in JAVA?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
To generate permutations of a character array in Java, use a recursive backtracking approach that swaps elements to explore all orderings. The base case prints or stores the permutation when the left index equals the right index. For arrays with duplicate characters, skip swaps where the same character would be placed at the same position to avoid duplicate permutations. The time complexity is O(n!) since there are n! permutations of n distinct elements.
Basic Recursive Permutation
The classic approach swaps each element into the current position, recurses on the remaining elements, then swaps back (backtracks):
Output:
Collecting Permutations in a List
Instead of printing, store results for later use:
Handling Duplicate Characters
When the array contains duplicate characters (e.g., {'A', 'A', 'B'}), the basic algorithm generates duplicate permutations. Use a HashSet to skip duplicates:
Permutations of Specific Elements Only
Permute only certain positions while keeping others fixed:
Iterative Approach (Next Permutation)
Generate permutations in lexicographic order without recursion:
K-Permutations (Choose k from n)
Generate all permutations of k elements from an array of n:
Common Pitfalls
- Not backtracking after the recursive call: Forgetting to swap back after the recursive call corrupts the array for subsequent iterations. Always pair each
swap(arr, left, i)with a matchingswap(arr, left, i)after the recursion. - Generating duplicate permutations with repeated characters: The basic algorithm treats identical characters as distinct, producing duplicates like "AAB" twice. Use a
HashSetto track which characters have already been placed at the current position. - Stack overflow on large arrays: The recursive approach uses O(n) stack depth. For arrays larger than ~10,000 elements (impractical for permutations due to O(n!) complexity), the JVM stack overflows. Use the iterative next-permutation approach for ordered traversal.
- Confusing permutations with combinations: Permutations care about order (AB != BA), combinations do not (AB == BA). If order does not matter, use a combination algorithm instead, which produces fewer results.
- Modifying the original array without copying: The swap-based approach modifies the input array in place. If you need the original array unchanged after generating permutations, pass a copy:
permute(arr.clone(), 0, arr.length - 1).
Summary
- Use recursive backtracking with swap to generate all permutations in O(n!) time
- Skip duplicate characters with a
HashSetat each recursion level to avoid duplicate permutations - Use the next-permutation algorithm for iterative, lexicographically ordered generation
- For k-permutations (choose k from n), stop the recursion at depth k instead of depth n
- Always backtrack (swap back) after the recursive call to restore the array state
Related reading
- Specific shuffling list in Python
- Split / Explode a column of dictionaries into separate columns with pandas
- Split a binary search Tree
- Split a List into smaller lists of N size
- Speed-up for finding an optimal partition line
- Splitting a number into the integer and decimal parts
- Specify either CPU or GPU for multiple models tensorflow java's job
- specify files in resources folder in spring application.properties file
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.