Listing all permutations of a string/integer
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
Permutations are a fundamental concept in mathematics and computer science. The permutation of a set is a rearrangement of its elements. In the context of strings or integers, it involves rearranging the characters or digits to form new combinations. Understanding how to generate permutations is essential for various applications, such as solving puzzles, generating test cases, or optimizing algorithms.
Technical Explanation
Definition and Mathematical Background
A permutation is any arrangement of a set of objects. Mathematically, if a set has elements, it has (n factorial) permutations. This factor represents the product of all positive integers from 1 through . For example, if you have a set of three elements {A, B, C}, the number of permutations is .
Algorithm Overview
To list all permutations of a string or integer:
- Recursive Approach:
- Choose a character from the string (or digit from an integer).
- Recursively generate permutations of the remaining characters.
- Append the chosen character to each permutation obtained from the recursive step.
- Iterative Approach:
- Use an iterative method such as the Heap's Algorithm, which is particularly efficient for generating permutations in a systematic way.
- Backtracking Approach:
- Use a decision tree to explore permutations by swapping elements, ensuring no duplicates are generated.
Complexity
The time complexity of generating permutations is , which is factorial in nature. Space complexity can be for storing the elements in a permutation.
Examples
Example 1: String Permutations
Consider the string "ABC". The permutations would be:
- ABC
- ACB
- BAC
- BCA
- CAB
- CBA
Here's a step-by-step breakdown using the recursive method:
- Fix 'A', permute
BCto getBC,CB. - Fix 'B', permute
ACto getAC,CA. - Fix 'C', permute
ABto getAB,BA.
Example 2: Integer Permutations
For the integer 123, the approach is identical to a string. The permutations are:
- 123
- 132
- 213
- 231
- 312
- 321
Recursive Code Implementation
Below is a Python implementation using the recursive method:
Using Libraries
In Python, we can use the built-in itertools module to generate permutations seamlessly:
Key Points Summary
| Feature | Description |
| Definition | Rearrangement of a set's elements to form new combinations. |
| Total Permutations | Calculated as (n factorial). |
| Approaches | Recursive, Iterative (e.g., Heap's), and Backtracking. |
| Complexity | Time: , Space: . |
| Python Library Support | Use itertools.permutations for
easy implementation. |
Conclusion
Understanding the permutations of a string or integer is crucial for numerous computational tasks. With both recursive and iterative methods at our disposal, and Python libraries simplifying the process, generating permutations becomes an accessible task for programmers. Whether for academic pursuits or solving complex real-world problems, mastery of permutations is an invaluable tool.
Related reading
- Locale based sort in Javascript, sort accented letters and other variants in a predefined way
- Lock-free algorithm library
- Lock-free Progress Guarantees in a circular buffer queue
- Locker Room Algorithm
- Logarithm Algorithm
- Longest equally-spaced subsequence
- log base 2 equals log base 3 when analyzing time complexity?
- Logic Solving Algorithm for Sudoku in Java

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.