String Combinations
Number Representation
Algorithm
Data Structures
Programming Techniques

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.

Practice algorithms

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:

  1. All permutations of the digits (without repetition).
  2. Combinations where digits can repeat.
  3. 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:

python
1def permute(s):
2    def backtrack(start, end):
3        if start == end:
4            print(''.join(s))
5            return
6        
7        for i in range(start, end):
8            # Swap the current element with the start
9            s[start], s[i] = s[i], s[start]
10            # Recurse for the rest
11            backtrack(start + 1, end)
12            # Backtrack to previous state
13            s[start], s[i] = s[i], s[start]
14    
15    backtrack(0, len(s))
16
17# Example usage
18permute(list("123"))

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.

python
1from itertools import combinations_with_replacement
2
3# Using combinations with replacement
4combs = combinations_with_replacement("12", 2)
5for comb in combs:
6    print(''.join(comb))

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:

python
1def letter_combinations(digits):
2    if not digits:
3        return []
4    
5    phone = {
6        "2": "abc", "3": "def", "4": "ghi",
7        "5": "jkl", "6": "mno", "7": "pqrs",
8        "8": "tuv", "9": "wxyz"
9    }
10    
11    def backtrack(index, path):
12        if index == len(digits):
13            combinations.append("".join(path))
14            return
15        
16        possible_letters = phone[digits[index]]
17        for letter in possible_letters:
18            path.append(letter)
19            backtrack(index + 1, path)
20            path.pop()
21    
22    combinations = []
23    backtrack(0, [])
24    return combinations
25
26# Usage
27print(letter_combinations("23"))

Key Points Table

VariationDescriptionExample InputExample Output
Permutations (no repetition)All possible orderings of digits"123"123, 132, 213, 231, 312, 321
Combinations with RepetitionCombinations allowing repeated digits"12", length 211, 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
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.