Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Letter Combinations of a Phone Number Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Letter Combinations of a Phone Number

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a string containing digits from 2 to 9 (inclusive), return all the possible letter combinations that the number could denote. The resultant list should be sorted lexicographically.

Approach

The approach is to store list of characters, matching with each of the numbers on the dial pad of the phone. Then for each digit in the given string, we try to match all the characters matching with it, with the previous digit in the given string, and generate all the possible strings in this way.

Analysis

  • Time Complexity: O(Product of size of the set of characters matched with each digit of the string)
  • Space Complexity: O(Product of size of the set of characters matched with each digit of the string)

Implementation

C++

vector<vector<char>> charSet;
vector<string> result;
vector<char> characterList;
void solve(string digits,int idx,int n){
	if(idx == n){
		string auxiliary = "";
		for(char i: characterList){
			auxiliary += i;
		}
		if(auxiliary.length() == digits.length()) {
			result.push_back(auxiliary);
		}
		return;
	}
	for(int i = idx; i < n; i++){
		for(char ch: charSet[digits[i] - '0']){
			characterList.push_back(ch);
			solve(digits, i + 1, n);
			characterList.pop_back();
		}
	}
}
vector<string> letterCombinations(string digits) {
	result.clear();
	charSet.clear();
	int n = digits.length();
	for(int i = 0; i <= 9; i++) {
		vector<char> temp;
		charSet.push_back(temp);
	}
	char ch = 'a';
	for(int i = 2; i <= 9; i++) {
		if(i != 7 && i != 9)
		for(int j = 0; j < 3; j++) {
			charSet[i].push_back(ch);
			ch++;
		}
		else{
			for(int j = 0;j < 4; j++){
				charSet[i].push_back(ch);
				ch++;
			}		
		}
	}
	solve(digits, 0, n);
	return result;
}
Java
class Solution {
	ArrayList<ArrayList<Character>> charSet;
	List<String> result;
	ArrayList<Character> characterList = new ArrayList<>();
	void solve(String digits,int idx,int n){
		if(idx == n){
			String auxiliary = "";
			for(char i: characterList){
				auxiliary += i;
			}
			if(auxiliary.length() == digits.length()) {
				result.add(auxiliary);
			}
			return;
		}
		for(int i = idx; i < n; i++){
			for(char ch: charSet.get(digits.charAt(i) - '0')){
				characterList.add(ch);
				solve(digits, i + 1, n);
				characterList.remove(characterList.size() - 1);
			}
		}
	}
	List<String> letterCombinations(String digits) {
		result = new ArrayList<>();
		charSet = new ArrayList<>();
		int n = digits.length();
		for(int i = 0; i <= 9; i++) {
			charSet.add(new ArrayList<>());
		}
		char ch = 'a';
		for(int i = 2; i <= 9; i++) {
			if(i != 7 && i != 9)
			for(int j = 0; j < 3; j++) {
				charSet.get(i).add(ch);
				ch++;
			}
			else{
				for(int j = 0;j < 4; j++){
					charSet.get(i).add(ch);
					ch++;
				}		
			}
		}
		solve(digits, 0, n);
		return result;
	}
}
Related Content
Combination Sum 1
Combination Sum 2
Combinations
Consecutively Descending Integers
Generate Parentheses
Kth Permutation Sequence
N Queens Problem
Palindrome Partitioning
Rat In A Maze
Restore IP Addresses
String Permutations
Subset Sum
Subsets
Subsets - II
Sudoku Solver
Tug of War
Word Break
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet