Skip to content

Commit e80131c

Browse files
authored
Merge pull request #1125 from MuraliDharan7/add-binarysearch
Binary Search Problems added
2 parents 1c69f22 + 7ebe0fb commit e80131c

File tree

5 files changed

+131
-65
lines changed

5 files changed

+131
-65
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
| Problem | Difficulty Level | Problem Link |
3+
|-----------------------------------------------------------|------------------|------------------------------------------------------------|
4+
| [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) |
5+
| [Implement Lower Bound](https://www.geeksforgeeks.org/implement-lower-bound-in-c/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/implement-lower-bound/1) |
6+
| [Implement Upper Bound](https://www.geeksforgeeks.org/implement-upper-bound-in-c/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/implement-upper-bound/1) |
7+
| [Search Insert Position](https://www.geeksforgeeks.org/search-insert-position/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/search-insert-position-of-k-in-a-sorted-array/1) |
8+
| [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.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
3+
| Problem | Difficulty Level | Problem Link |
4+
|-----------------------------------------------------------|------------------|------------------------------------------------------------|
5+
| [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) |
11+
| [Kth Missing Positive Number](https://www.geeksforgeeks.org/kth-missing-positive-number/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/kth-missing-positive-number/1) |
12+
| [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.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
| Problem | Difficulty Level | Problem Link |
3+
|-----------------------------------------------------------|------------------|------------------------------------------------------------|
4+
| [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) |
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Binary Search Problems",
3+
"position": 3,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Here You can Solve all of the Problems Which uses Binary Search."
7+
}
8+
}

dsa-solutions/lc-solutions/0000-0099/0017-letter-combinations-of-a-phone-number.md

Lines changed: 75 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@ id: letter-combinations-of-a-phone-number
33
title: Letter Combinations of a Phone Number (LeetCode)
44
sidebar_label: 0017 Letter Combinations of a Phone Number
55
tags:
6-
- Back Tracking
7-
- Mapping
8-
- String
6+
- Back Tracking
7+
- Mapping
8+
- String
99
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
1011
---
1112

1213
## Problem Description
1314

14-
| Problem Statement | Solution Link | LeetCode Profile |
15-
| :----------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------- |
16-
| [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 |
16+
| :------------------------------------------------------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :----------------------------------------------------- |
17+
| [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/) |
1718

1819
### Problem Description
1920

2021
## Problem Statement:
22+
2123
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.
2224

2325
### Examples
@@ -32,7 +34,6 @@ Given a string containing digits from 2-9 inclusive, return all possible letter
3234
- **Input:** `digits = ""`
3335
- **Output:** `[]`
3436

35-
3637
#### Example 3
3738

3839
- **Input:** `2`
@@ -47,9 +48,11 @@ Given a string containing digits from 2-9 inclusive, return all possible letter
4748
### Approach
4849

4950
1. **Mapping Digits to Letters:**
51+
5052
- Define a mapping of digits to their corresponding letters, similar to telephone buttons.
5153

5254
2. **Backtracking Function:**
55+
5356
- Define a recursive backtracking function to generate all possible combinations.
5457
- The function takes four parameters:
5558
- `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
5962
- After the recursive call, we remove the last character from the combination (backtracking).
6063

6164
3. **Base Case:**
65+
6266
- 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.
6367

6468
4. **Main Function:**
@@ -153,6 +157,7 @@ public class Solution {
153157
```
154158

155159
#### CPP:
160+
156161
```cpp
157162
#include <iostream>
158163
#include <vector>
@@ -208,80 +213,82 @@ int main() {
208213
```
209214
210215
#### JavaScript
216+
211217
```js
212218
/**
213219
* @param {string} digits
214220
* @return {string[]}
215221
*/
216-
var letterCombinations = function(digits) {
217-
if (digits.length === 0) return [];
218-
219-
const digitToLetters = {
220-
'2': 'abc',
221-
'3': 'def',
222-
'4': 'ghi',
223-
'5': 'jkl',
224-
'6': 'mno',
225-
'7': 'pqrs',
226-
'8': 'tuv',
227-
'9': 'wxyz'
228-
};
229-
230-
const combinations = [];
231-
232-
const backtrack = (index, path) => {
233-
if (index === digits.length) {
234-
combinations.push(path);
235-
return;
236-
}
237-
const letters = digitToLetters[digits.charAt(index)];
238-
for (let letter of letters) {
239-
backtrack(index + 1, path + letter);
240-
}
241-
};
242-
243-
backtrack(0, '');
244-
return combinations;
222+
var letterCombinations = function (digits) {
223+
if (digits.length === 0) return [];
224+
225+
const digitToLetters = {
226+
2: "abc",
227+
3: "def",
228+
4: "ghi",
229+
5: "jkl",
230+
6: "mno",
231+
7: "pqrs",
232+
8: "tuv",
233+
9: "wxyz",
234+
};
235+
236+
const combinations = [];
237+
238+
const backtrack = (index, path) => {
239+
if (index === digits.length) {
240+
combinations.push(path);
241+
return;
242+
}
243+
const letters = digitToLetters[digits.charAt(index)];
244+
for (let letter of letters) {
245+
backtrack(index + 1, path + letter);
246+
}
247+
};
248+
249+
backtrack(0, "");
250+
return combinations;
245251
};
246252
247253
// Example usage:
248254
console.log(letterCombinations("23")); // Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
249255
```
250256

251257
#### TypeScript
258+
252259
```ts
253260
class Solution {
254-
private digitToLetters: { [key: string]: string } = {
255-
'2': 'abc',
256-
'3': 'def',
257-
'4': 'ghi',
258-
'5': 'jkl',
259-
'6': 'mno',
260-
'7': 'pqrs',
261-
'8': 'tuv',
262-
'9': 'wxyz'
261+
private digitToLetters: { [key: string]: string } = {
262+
"2": "abc",
263+
"3": "def",
264+
"4": "ghi",
265+
"5": "jkl",
266+
"6": "mno",
267+
"7": "pqrs",
268+
"8": "tuv",
269+
"9": "wxyz",
270+
};
271+
272+
letterCombinations(digits: string): string[] {
273+
const combinations: string[] = [];
274+
275+
const backtrack = (index: number, path: string): void => {
276+
if (index === digits.length) {
277+
combinations.push(path);
278+
return;
279+
}
280+
const letters = this.digitToLetters[digits.charAt(index)];
281+
for (let letter of letters) {
282+
backtrack(index + 1, path + letter);
283+
}
263284
};
264285

265-
letterCombinations(digits: string): string[] {
266-
const combinations: string[] = [];
267-
268-
const backtrack = (index: number, path: string): void => {
269-
if (index === digits.length) {
270-
combinations.push(path);
271-
return;
272-
}
273-
const letters = this.digitToLetters[digits.charAt(index)];
274-
for (let letter of letters) {
275-
backtrack(index + 1, path + letter);
276-
}
277-
};
278-
279-
if (digits.length !== 0) {
280-
backtrack(0, '');
281-
}
282-
283-
return combinations;
286+
if (digits.length !== 0) {
287+
backtrack(0, "");
284288
}
289+
290+
return combinations;
291+
}
285292
}
286293

287294
// Example usage:
@@ -294,9 +301,11 @@ console.log(solution.letterCombinations("23")); // Output: ["ad","ae","af","bd",
294301
Here's a step-by-step algorithm for generating all possible letter combinations of a given string of digits using backtracking:
295302

296303
1. **Define a mapping of digits to letters:**
304+
297305
- Create a map where each digit from 2 to 9 is mapped to its corresponding letters on a telephone keypad.
298306

299307
2. **Define a backtracking function:**
308+
300309
- The function will take the following parameters:
301310
- `index`: The current index in the digits string.
302311
- `path`: The current combination of letters.
@@ -305,11 +314,12 @@ Here's a step-by-step algorithm for generating all possible letter combinations
305314
- After the recursive call, remove the last character from the combination (backtracking).
306315

307316
3. **Base Case:**
317+
308318
- If the length of the current combination is equal to the length of the input digits string, add the combination to the result list.
309319

310320
4. **Main Function:**
311321
- Initialize an empty list to store the combinations.
312322
- Call the backtracking function with the initial index set to 0 and an empty string as the initial combination.
313323
- Return the list of combinations.
314324

315-
This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking.
325+
This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking.

0 commit comments

Comments
 (0)