Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

17. Letter Combinations of a Phone Number

Medium62.3% Acceptance
Hash TableStringBacktracking
Asked by:
Amazon
ProblemSolutions (12)VideosCompanies (35)Notes

Problem Statement

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.

A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

Example 1:

Input: digits = "23"
Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]

Example 2:

Input: digits = ""
Output: []

Example 3:

Input: digits = "2"
Output: ["a","b","c"]

Constraints:

  • 0 <= digits.length <= 4
  • digits[i] is a digit in the range ['2', '9'].
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
F
Flipkart
A
Atlassian
C
Capital One
D
Databricks
+30

Approach

The key idea behind #17 Letter Combinations of a Phone Number is to simulate the mapping of digits to letters on a traditional phone keypad. Each digit from 2-9 corresponds to a set of characters. The task is to generate all possible letter combinations that the input digit string could represent.

A common and efficient strategy is backtracking. First, store the digit-to-letter mapping in a hash table or array. Then recursively build combinations by selecting one letter for the current digit and moving to the next digit. When the length of the current combination matches the input length, add it to the result list.

This approach systematically explores every possible combination. Because each digit can map to up to 4 letters, the number of combinations grows exponentially. The time complexity is approximately O(4^n * n), where n is the number of digits, while the recursion stack contributes O(n) auxiliary space.

Complexity

ApproachTime ComplexitySpace Complexity
Backtracking with digit-to-letter mappingO(4^n * n)O(n)
BFS / Iterative combination buildingO(4^n * n)O(4^n)

Video Solution Available

NeetCode

View all video solutions

Solutions (12)

Recursive Backtracking Approach

This approach uses recursion to explore all possible combinations represented by the digit-to-letter mapping of a phone keypad. We incrementally build letter combinations by allocating one letter per digit, and then explore further by making recursive calls.

The recursion depth is determined by the number of digits and the choices per digit. At each step, the algorithm branches into as many recursive calls as there are letters assigned to the current digit, continuing until all digits have been processed.

Time Complexity: O(3^n * 4^m), where n is the number of digits that map to 3 letters, and m is the number of digits that map to 4 letters.

Space Complexity: O(n), the maximum depth of the recursion stack is n.

PythonJavaC++CC#JavaScript
1def letterCombinations(digits):
2    if not digits:
3        return []
4    
5    phone_map = {'2':                 
        

Explanation

This Python solution utilizes a recursive helper method backtrack to explore all combinations by trying each possible letter for each digit sequentially. The base case is when the length of the path equals the number of digits, signifying a complete combination has been constructed. The combinations list accumulates all completed combinations, which is then returned as the result.

Iterative Breadth-First Search (BFS) Approach

The iterative BFS approach systematically explores all possible combinations of letter sequences by progressively building up combinations using a queue. It starts by enqueueing an empty string, and then, for each digit from the input, extends through all mapped letters, enqueuing partial solutions until all digits have been processed.

This method mimics the complete depth-first search but layers on combinations iteratively, capitalizing on the breadth search over immediate digit map expansion.

Time Complexity: O(3^n * 4^m), as methodically layers through the digit-letter expansion uniformly across a queue.

Space Complexity: O(n * 3^n * 4^m), where queue storage inflates in tandem with combination depth.

PythonJavaC++CC#JavaScript





Video Solutions

Watch expert explanations and walkthroughs

Letter Combinations of a Phone Number - Backtracking - Leetcode 17

NeetCode
11:15186,659 views

Asked By Companies

35 companies
A
Amazon
F
Flipkart
A
Atlassian
C
Capital One
D
Databricks
e
eBay
M
Morgan Stanley
O
Oracle
Q
Qualtrics
T
Twilio
V
VMware
W
Walmart Labs
M
Microsoft
D
Dunzo
o
oyo
O
Ola
H
Hexaware Tech
G
Goldman Sachs
C
Cisco
Z
Zepto
B
Bloomreach
S
Spinny
N
Nagarro
B
Blue Yonder
F
Facebook
E
EPAM Systems
Z
Zoho Corporation
V
VFISLK Global Services
A
Airtel
U
Uber
A
Apple
G
Google
B
Bloomberg
E
Epic Systems
S
Swiggy

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Longest Substring Without Repeating CharactersMedium
Integer to RomanMedium
Roman to IntegerEasy
Substring with Concatenation of All WordsHard
More similar problems

Related Topics

Hash TableStringBacktracking

Problem Stats

Acceptance Rate62.3%
DifficultyMedium
Companies35

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Letter Combinations of a Phone Number asked in FAANG interviews?

Yes, this problem is frequently asked in technical interviews at companies like Google, Amazon, and Meta. It tests understanding of recursion, backtracking, and combinatorial generation using strings.

Can Letter Combinations of a Phone Number be solved without recursion?

Yes, it can also be solved using an iterative BFS-style approach. In this method, you build combinations level by level using a queue or list, expanding each partial string with letters mapped to the next digit.

What data structure is best for Letter Combinations of a Phone Number?

A hash table or array is typically used to store the mapping between digits and their corresponding letters on a phone keypad. This allows constant-time access when expanding combinations during recursion or iteration.

What is the optimal approach for Letter Combinations of a Phone Number?

The optimal approach uses backtracking. By mapping each digit to its corresponding letters and recursively building combinations, you can systematically generate all possible strings. This approach is efficient and commonly expected in coding interviews.

Previous Problem

Roman to Integer

Next Problem

Substring with Concatenation of All Words

'abc'
,
'3'
:
'def'
,
'4'
:
'ghi'
,
'5'
:
'jkl'
,
6
'6'
:
'mno'
,
'7'
:
'pqrs'
,
'8'
:
'tuv'
,
'9'
:
'wxyz'
}
7
8
def
backtrack
(
index
,
path
)
:
9
# If the current combination is complete
10
if
len
(
path
)
==
len
(
digits
)
:
11
combinations
.
append
(
''
.
join
(
path
)
)
12
return
13
14
# Get the letters that the current digit maps to, and loop through them
15
possible_letters
=
phone_map
[
digits
[
index
]
]
16
for
letter
in
possible_letters
:
17
# Add the letter to our current path and move to the next digit
18
path
.
append
(
letter
)
19
backtrack
(
index
+
1
,
path
)
20
path
.
pop
(
)
# Backtrack by removing the letter
21
22
combinations
=
[
]
23
backtrack
(
0
,
[
]
)
24
return
combinations
25
# Example use
26
print
(
letterCombinations
(
'23'
)
)
1
from
collections
import
deque
2
3
def
letterCombinations
(
digits
)
:
4
if
not
digits
:
5
return
[
]
6
7
phone_map
=
{
'2'
:
'abc'
,
'3'
:
'def'
,
'4'
:
'ghi'
,
'5'
:
'jkl'
,
8
'6'
:
'mno'
,
'7'
:
'pqrs'
,
'8'
:
'tuv'
,
'9'
:
'wxyz'
}
9
10
combinations
=
deque
(
[
''
]
)
11
for
digit
in
digits
:
12
level_size
=
len
(
combinations
)
13
for
_
in
range
(
level_size
)
:
14
prev_combination
=
combinations
.
popleft
(
)
15
for
letter
in
phone_map
[
digit
]
:
16
combinations
.
append
(
prev_combination
+
letter
)
17
18
return
list
(
combinations
)
19
20
# Example use
21
print
(
letterCombinations
(
'23'
)
)

Explanation

The Python solution leverages a queue (deque) to perform BFS-style exploration of all letter combinations. Initially, it stores an empty string, and iteratively enqueues combinations by appending the letters mapped from current digits. The process continues until all digits have been covered, emerging with a full spectrum of possible sequences.