Find all possible combinations of a String representation of a number
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Overview
Finding all possible combinations of a string representation of a number is a common problem in computer science, particularly in contexts like generating permutations of phone numbers or decoding messages using numbers as codes. This process involves generating different sequences or arrangements where the order may or may not matter depending on the application.
Problem Definition
Given a string representation of a number, our task is to determine all possible combinations of its digits. This problem can be broken down into several variations, including:
- All permutations of the digits (without repetition).
- Combinations where digits can repeat.
- Deriving possible numbers from a sequence using a mapping, such as a telephone keypad where each number represents a set of letters.
Technical Explanation
1. All Permutations
Permutations refer to the rearrangement of a set of items (in this case, digits of the number), where the order matters and elements do not repeat.
Example
For the number "123", the permutations are:
- 123
- 132
- 213
- 231
- 312
- 321
Approach
To generate permutations, one could use a recursive backtracking algorithm. Here's a simple implementation in Python:
2. Combinations with Repetition
Unlike permutations, combinations with repetition consider scenarios where digits can repeat. For instance, finding combinations of a number that represents the total count of certain items.
Example
For the number "12" and combination length of 2:
- 11
- 12
- 22
Approach
This can be solved using a recursive approach or utilizing tools like Python’s itertools.combinations_with_replacement.
3. Mapping Techniques
Mapping each digit to different characters or codes, such as keypads where each number corresponds to several letters (e.g., '2' can map to 'a', 'b', 'c'), adds a layer of complexity and depends largely on context, such as interpreting phone numbers.
Example
The string "23" can be decoded leveraging a phone keypad mapping:
- "ad", "ae", "af"
- "bd", "be", "bf"
- "cd", "ce", "cf"
Approach
We can solve this using recursive backtracking or using queue-based breadth-first search (BFS). Below is a simple backtracking implementation:
Key Points Table
| Variation | Description | Example Input | Example Output |
| Permutations (no repetition) | All possible orderings of digits | "123" | 123, 132, 213, 231, 312, 321 |
| Combinations with Repetition | Combinations allowing repeated digits | "12", length 2 | 11, 12, 22 |
| Mapping (Phone Keypad) | Mapping digits to possible letters | "23" | "ad", "ae", "af", ... "cf" |
Conclusion
Understanding how to find all possible combinations of a string representation of a number is a powerful skill in computational logic and programming. Different variations of this problem include permutations, combinations with or without repetition, and mappings like those used in phone keypads. Mastering these techniques involves understanding recursive backtracking, the use of combinatorial libraries, and mapping logic. These skills can be widely applied across algorithms, data processing, and application development.
Related reading
- Find all possible substring in fastest way
- Find all subsets of length k in an array
- find all subsets that sum to a particular value
- Find all substrings that are palindromes
- Find all the paths forming simple cycles on an undirected graph
- Find all triplets in array with sum less than or equal to given sum
- Find an algorithm to balance this game
- Find an algorithm to win this battle against crime!

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.