Finding all the unique permutations of a string without generating duplicates
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Finding all unique permutations of a string is a classic computational problem that blends combinatorics with practical programming. In simple terms, a permutation refers to any arrangement of the elements of a string. For instance, the string "ABC" can be rearranged as "BAC", "CBA", and so forth. However, when a string contains repeated characters, generating permutations without duplicating results becomes challenging. This article will explore methods to obtain unique permutations efficiently, without generating duplicates.
The Permutation Problem
Permutations of a string involving repeated characters pose unique challenges. Consider the string "AAB". Although it is made of distinct permutations like "ABA", "BAA", generating permutations without accounting for duplicates is computationally suboptimal and wasteful.
Mathematical Foundation
The formula for the number of unique permutations of a string is given by:
Here, is the total number of characters in the string, and are the frequencies of each distinct character in the string.
Example
For the string "AAB":
• Total characters = 3 (A, A, B) • Frequencies = (, )
Unique permutations =
Implementation Approach
Algorithm Explanation
To find all unique permutations without duplicates:
- Sort the Input String: Begin with a sorted version of the string to handle duplicates easily.
- Backtracking: Use a recursive function that explores all potential character combinations.
- Avoiding Duplicates: Employ a boolean array `used` to track whether a specific character has already been used at the current recursion level. Skip any character that has been used.
Example Code
Below is a Python implementation using backtracking:
• Prune Non-Promising Paths: By sorting the array and skipping characters that have already been placed in the permutation, non-promising paths are pruned early. • Iterative Approach: Beyond backtracking, permutations can be achieved iteratively using data structures like stacks, though recursion is more intuitive for this problem domain. • Dictionary for Frequency Counting: Use a dictionary to count character frequencies, reducing repeated computations.
Related reading
- Finding an axis-aligned rectangle inside a polygon
- Finding an element in an array where every element is repeated odd number of times but more than single occurrence and only one appears once
- Finding an number in montonically increasing and then decreasing sequencecera
- Finding an optimal solution that minimizes a constraint?
- Finding centre of rotation for a set of points
- Finding complete rectangles enclosing 0
- Finding blocks in arrays
- Finding bridges in graph without recursion

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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.