You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|[Binary Search to find X in sorted array](https://www.geeksforgeeks.org/binary-search/)| Easy |[Problem Link](https://practice.geeksforgeeks.org/problems/binary-search-1587115620/1)|
|[Floor/Ceil in Sorted Array](https://www.geeksforgeeks.org/floor-and-ceil-from-a-sorted-array/)| Medium |[Problem Link](https://practice.geeksforgeeks.org/problems/floor-in-a-sorted-array-1587115620/1)|
9
+
|[Find the first or last occurrence of a given number in a sorted array](https://www.geeksforgeeks.org/find-first-and-last-positions-of-an-element-in-a-sorted-array/)| Easy |[Problem Link](https://practice.geeksforgeeks.org/problems/first-and-last-occurrences-of-x/0)|
10
+
|[Count occurrences of a number in a sorted array with duplicates](https://www.geeksforgeeks.org/count-number-of-occurrences-or-frequency-in-a-sorted-array/)| Easy |[Problem Link](https://practice.geeksforgeeks.org/problems/number-of-occurrence/0)|
11
+
|[Search in Rotated Sorted Array I](https://www.geeksforgeeks.org/search-an-element-in-a-sorted-and-pivoted-array/)| Medium |[Problem Link](https://practice.geeksforgeeks.org/problems/search-in-a-rotated-array/0)|
12
+
|[Search in Rotated Sorted Array II](https://www.geeksforgeeks.org/search-an-element-in-a-sorted-and-rotated-array-2/)| Medium |[Problem Link](https://practice.geeksforgeeks.org/problems/search-in-a-rotated-array-2/1)|
13
+
|[Find minimum in Rotated Sorted Array](https://www.geeksforgeeks.org/find-minimum-element-in-a-sorted-and-rotated-array/)| Medium |[Problem Link](https://practice.geeksforgeeks.org/problems/minimum-element-in-a-sorted-and-rotated-array/0)|
14
+
|[Find out how many times has an array been rotated](https://www.geeksforgeeks.org/find-rotation-count-rotated-sorted-array/)| Easy |[Problem Link](https://practice.geeksforgeeks.org/problems/rotation/0)|
15
+
|[Single element in a Sorted Array](https://www.geeksforgeeks.org/find-the-element-that-appears-once-in-a-sorted-array/)| Easy |[Problem Link](https://practice.geeksforgeeks.org/problems/find-the-element-that-appears-once-in-sorted-array/0)|
16
+
|[Find peak element](https://www.geeksforgeeks.org/find-a-peak-in-a-given-array/)| Hard |[Problem Link](https://practice.geeksforgeeks.org/problems/peak-element/1)|
17
+
18
+
19
+
This table includes the problem name with a link to its article on GeeksforGeeks, the difficulty level, and a link to the problem itself on GeeksforGeeks.
|[Find square root of a number in log n](https://www.geeksforgeeks.org/square-root-of-an-integer/)| Medium |[Problem Link](https://practice.geeksforgeeks.org/problems/square-root-of-a-number/0)|
6
+
|[Find the Nth root of a number using binary search](https://www.geeksforgeeks.org/n-th-root-number-using-logarithm/)| Medium |[Problem Link](https://practice.geeksforgeeks.org/problems/nth-root-of-m/0)|
7
+
|[Koko Eating Bananas](https://www.geeksforgeeks.org/koko-eating-bananas/)| Hard |[Problem Link](https://practice.geeksforgeeks.org/problems/koko-eating-bananas/1)|
8
+
|[Minimum days to make M bouquets](https://www.geeksforgeeks.org/minimum-days-to-make-m-bouquets/)| Hard |[Problem Link](https://practice.geeksforgeeks.org/problems/minimum-days-to-make-m-bouquets/1)|
9
+
|[Find the smallest Divisor](https://www.geeksforgeeks.org/find-the-smallest-divisor-given-a-threshold/)| Easy |[Problem Link](https://practice.geeksforgeeks.org/problems/smallest-divisor/1)|
10
+
|[Capacity to Ship Packages within D Days](https://www.geeksforgeeks.org/capacity-to-ship-packages-within-d-days/)| Hard |[Problem Link](https://practice.geeksforgeeks.org/problems/capacity-to-ship-packages-within-d-days/1)|
|[Aggressive Cows](https://www.geeksforgeeks.org/aggressive-cows-problem/)| Hard |[Problem Link](https://practice.geeksforgeeks.org/problems/aggressive-cows/0)|
13
+
|[Book Allocation Problem](https://www.geeksforgeeks.org/allocate-minimum-number-pages/)| Hard |[Problem Link](https://practice.geeksforgeeks.org/problems/allocate-minimum-number-of-pages/0)|
14
+
|[Split array - Largest Sum](https://www.geeksforgeeks.org/split-array-largest-sum/)| Hard |[Problem Link](https://practice.geeksforgeeks.org/problems/split-array-largest-sum/1)|
15
+
|[Painter's partition](https://www.geeksforgeeks.org/painters-partition-problem/)| Hard |[Problem Link](https://practice.geeksforgeeks.org/problems/the-painters-partition-problem/0)|
16
+
|[Minimize Max Distance to Gas Station](https://www.geeksforgeeks.org/minimize-max-distance-to-gas-station/)| Hard |[Problem Link](https://practice.geeksforgeeks.org/problems/minimize-max-distance-to-gas-station/1)|
17
+
|[Median of 2 sorted arrays](https://www.geeksforgeeks.org/median-of-two-sorted-arrays-of-different-sizes/)| Hard |[Problem Link](https://practice.geeksforgeeks.org/problems/median-of-2-sorted-arrays-of-different-sizes/1)|
18
+
|[Kth element of 2 sorted arrays](https://www.geeksforgeeks.org/k-th-element-two-sorted-arrays/)| Medium |[Problem Link](https://practice.geeksforgeeks.org/problems/k-th-element-of-two-sorted-arrays/0)|
19
+
20
+
21
+
This table includes the problem name with a link to its article on GeeksforGeeks, the difficulty level, and a link to the problem itself on GeeksforGeeks.
|[Find the row with maximum number of 1's](https://www.geeksforgeeks.org/find-the-row-with-maximum-number-of-1s/)| Easy |[Problem Link](https://practice.geeksforgeeks.org/problems/row-with-max-1s0023/1)|
5
+
|[Search in a 2D matrix](https://www.geeksforgeeks.org/search-in-a-row-wise-and-column-wise-sorted-2d-array/)| Medium |[Problem Link](https://practice.geeksforgeeks.org/problems/search-in-a-matrix-1587115621/1)|
6
+
|[Search in a row and column wise sorted matrix](https://www.geeksforgeeks.org/search-in-row-wise-and-column-wise-sorted-matrix/)| Medium |[Problem Link](https://practice.geeksforgeeks.org/problems/search-in-a-matrix-1728441/1)|
7
+
|[Find Peak Element (2D Matrix)](https://www.geeksforgeeks.org/find-a-peak-element-in-a-2d-array/)| Hard |[Problem Link](https://practice.geeksforgeeks.org/problems/find-peak-element-2d/1)|
8
+
|[Matrix Median](https://www.geeksforgeeks.org/find-median-row-wise-sorted-matrix/)| Hard |[Problem Link](https://practice.geeksforgeeks.org/problems/median-in-a-row-wise-sorted-matrix1527/1)|
title: Letter Combinations of a Phone Number (LeetCode)
4
4
sidebar_label: 0017 Letter Combinations of a Phone Number
5
5
tags:
6
-
- Back Tracking
7
-
- Mapping
8
-
- String
6
+
- Back Tracking
7
+
- Mapping
8
+
- String
9
9
description: The problem requires generating all letter combinations corresponding to given digits (2-9). The solution utilizes backtracking to explore all combinations efficiently, employing a recursive approach in Java.
10
+
sidebar_position: 17
10
11
---
11
12
12
13
## Problem Description
13
14
14
-
| Problem Statement | Solution Link | LeetCode Profile |
|[Letter Combinations of a Phone Number](https://leetcode.com/problems/Letter Combinations of a Phone Number/) |[Letter Combinations of a Phone Number Solution on LeetCode](https://leetcode.com/problems/Letter Combinations of a Phone Number/solutions/5055810/video-two-pointer-solution/) |[gabaniyash846](https://leetcode.com/u/gabaniyash846/)|
15
+
| Problem Statement | Solution Link | LeetCode Profile|
|[Letter Combinations of a Phone Number](https://leetcode.com/problems/Letter Combinations of a Phone Number/) |[Letter Combinations of a Phone Number Solution on LeetCode](https://leetcode.com/problems/Letter Combinations of a Phone Number/solutions/5055810/video-two-pointer-solution/) |[gabaniyash846](https://leetcode.com/u/gabaniyash846/)|
17
18
18
19
### Problem Description
19
20
20
21
## Problem Statement:
22
+
21
23
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.
22
24
23
25
### Examples
@@ -32,7 +34,6 @@ Given a string containing digits from 2-9 inclusive, return all possible letter
32
34
-**Input:**`digits = ""`
33
35
-**Output:**`[]`
34
36
35
-
36
37
#### Example 3
37
38
38
39
-**Input:**`2`
@@ -47,9 +48,11 @@ Given a string containing digits from 2-9 inclusive, return all possible letter
47
48
### Approach
48
49
49
50
1.**Mapping Digits to Letters:**
51
+
50
52
- Define a mapping of digits to their corresponding letters, similar to telephone buttons.
51
53
52
54
2.**Backtracking Function:**
55
+
53
56
- Define a recursive backtracking function to generate all possible combinations.
54
57
- The function takes four parameters:
55
58
-`index`: The current index in the digits string.
@@ -59,6 +62,7 @@ Given a string containing digits from 2-9 inclusive, return all possible letter
59
62
- After the recursive call, we remove the last character from the combination (backtracking).
60
63
61
64
3.**Base Case:**
65
+
62
66
- If the length of the current combination is equal to the length of the input digits string, we add the combination to the result list.
0 commit comments