Permutations
Unique Permutations
String Algorithms
No Duplicates
Combinatorics

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.

Practice algorithms

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:

n!n_1!×n_2!××n_k!\frac{n!}{n\_1! \times n\_2! \times \ldots \times n\_k!}

Here, nn is the total number of characters in the string, and n1,n2,,nkn_1, n_2, \ldots, n_k are the frequencies of each distinct character in the string.

Example

For the string "AAB":

• Total characters = 3 (A, A, B) • Frequencies = (nA=2n_A = 2, nB=1n_B = 1)

Unique permutations = 3!2!×1!=3\frac{3!}{2! \times 1!} = 3

Implementation Approach

Algorithm Explanation

To find all unique permutations without duplicates:

  1. Sort the Input String: Begin with a sorted version of the string to handle duplicates easily.
  2. Backtracking: Use a recursive function that explores all potential character combinations.
  3. 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.