From d6d04a115af8d263d63c90e4ad6d07132d36ad09 Mon Sep 17 00:00:00 2001 From: Nishant Kaushal <101548649+nishant0708@users.noreply.github.com> Date: Tue, 11 Jun 2024 22:08:08 +0530 Subject: [PATCH 1/2] Updated solution for 0017-Letter-Combinations-of-a-Phone-Number --- ...7-letter-combinations-of-a-phone-number.md | 140 ++++++++---------- 1 file changed, 65 insertions(+), 75 deletions(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0017-letter-combinations-of-a-phone-number.md b/dsa-solutions/lc-solutions/0000-0099/0017-letter-combinations-of-a-phone-number.md index 90b0bd8a5..890c371f1 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0017-letter-combinations-of-a-phone-number.md +++ b/dsa-solutions/lc-solutions/0000-0099/0017-letter-combinations-of-a-phone-number.md @@ -3,23 +3,21 @@ id: letter-combinations-of-a-phone-number title: Letter Combinations of a Phone Number (LeetCode) sidebar_label: 0017 Letter Combinations of a Phone Number tags: - - Back Tracking - - Mapping - - String + - Back Tracking + - Mapping + - String 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. -sidebar_position: 17 --- ## Problem Description -| 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/) | +| 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/) | ### Problem Description ## 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. ### Examples @@ -34,6 +32,7 @@ Given a string containing digits from 2-9 inclusive, return all possible letter - **Input:** `digits = ""` - **Output:** `[]` + #### Example 3 - **Input:** `2` @@ -49,11 +48,9 @@ Given a string containing digits from 2-9 inclusive, return all possible letter ### Approach 1. **Mapping Digits to Letters:** - - Define a mapping of digits to their corresponding letters, similar to telephone buttons. 2. **Backtracking Function:** - - Define a recursive backtracking function to generate all possible combinations. - The function takes four parameters: - `index`: The current index in the digits string. @@ -63,7 +60,6 @@ Given a string containing digits from 2-9 inclusive, return all possible letter - After the recursive call, we remove the last character from the combination (backtracking). 3. **Base Case:** - - 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. 4. **Main Function:** @@ -159,7 +155,6 @@ public class Solution { ``` #### CPP: - ```cpp #include #include @@ -215,41 +210,40 @@ int main() { ``` #### JavaScript - ```js /** * @param {string} digits * @return {string[]} */ -var letterCombinations = function (digits) { - if (digits.length === 0) return []; - - const digitToLetters = { - 2: "abc", - 3: "def", - 4: "ghi", - 5: "jkl", - 6: "mno", - 7: "pqrs", - 8: "tuv", - 9: "wxyz", - }; - - const combinations = []; - - const backtrack = (index, path) => { - if (index === digits.length) { - combinations.push(path); - return; - } - const letters = digitToLetters[digits.charAt(index)]; - for (let letter of letters) { - backtrack(index + 1, path + letter); - } - }; - - backtrack(0, ""); - return combinations; +var letterCombinations = function(digits) { + if (digits.length === 0) return []; + + const digitToLetters = { + '2': 'abc', + '3': 'def', + '4': 'ghi', + '5': 'jkl', + '6': 'mno', + '7': 'pqrs', + '8': 'tuv', + '9': 'wxyz' + }; + + const combinations = []; + + const backtrack = (index, path) => { + if (index === digits.length) { + combinations.push(path); + return; + } + const letters = digitToLetters[digits.charAt(index)]; + for (let letter of letters) { + backtrack(index + 1, path + letter); + } + }; + + backtrack(0, ''); + return combinations; }; // Example usage: @@ -257,40 +251,39 @@ console.log(letterCombinations("23")); // Output: ["ad","ae","af","bd","be","bf" ``` #### TypeScript - ```ts class Solution { - private digitToLetters: { [key: string]: string } = { - "2": "abc", - "3": "def", - "4": "ghi", - "5": "jkl", - "6": "mno", - "7": "pqrs", - "8": "tuv", - "9": "wxyz", - }; - - letterCombinations(digits: string): string[] { - const combinations: string[] = []; - - const backtrack = (index: number, path: string): void => { - if (index === digits.length) { - combinations.push(path); - return; - } - const letters = this.digitToLetters[digits.charAt(index)]; - for (let letter of letters) { - backtrack(index + 1, path + letter); - } + private digitToLetters: { [key: string]: string } = { + '2': 'abc', + '3': 'def', + '4': 'ghi', + '5': 'jkl', + '6': 'mno', + '7': 'pqrs', + '8': 'tuv', + '9': 'wxyz' }; - if (digits.length !== 0) { - backtrack(0, ""); - } + letterCombinations(digits: string): string[] { + const combinations: string[] = []; + + const backtrack = (index: number, path: string): void => { + if (index === digits.length) { + combinations.push(path); + return; + } + const letters = this.digitToLetters[digits.charAt(index)]; + for (let letter of letters) { + backtrack(index + 1, path + letter); + } + }; - return combinations; - } + if (digits.length !== 0) { + backtrack(0, ''); + } + + return combinations; + } } // Example usage: @@ -303,11 +296,9 @@ console.log(solution.letterCombinations("23")); // Output: ["ad","ae","af","bd", Here's a step-by-step algorithm for generating all possible letter combinations of a given string of digits using backtracking: 1. **Define a mapping of digits to letters:** - - Create a map where each digit from 2 to 9 is mapped to its corresponding letters on a telephone keypad. 2. **Define a backtracking function:** - - The function will take the following parameters: - `index`: The current index in the digits string. - `path`: The current combination of letters. @@ -316,7 +307,6 @@ Here's a step-by-step algorithm for generating all possible letter combinations - After the recursive call, remove the last character from the combination (backtracking). 3. **Base Case:** - - If the length of the current combination is equal to the length of the input digits string, add the combination to the result list. 4. **Main Function:** @@ -324,4 +314,4 @@ Here's a step-by-step algorithm for generating all possible letter combinations - Call the backtracking function with the initial index set to 0 and an empty string as the initial combination. - Return the list of combinations. -This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking. +This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking. \ No newline at end of file From e51a0960d338534738aaede72b1c46a6436fba5b Mon Sep 17 00:00:00 2001 From: Nishant Kaushal <101548649+nishant0708@users.noreply.github.com> Date: Thu, 13 Jun 2024 20:49:16 +0530 Subject: [PATCH 2/2] Added Q263and264 --- .../0200-0299/0263-Ugly-Number.md | 122 ++++++++++++ .../0200-0299/0264-Ugly-Number-II.md | 175 ++++++++++++++++++ 2 files changed, 297 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0200-0299/0263-Ugly-Number.md create mode 100644 dsa-solutions/lc-solutions/0200-0299/0264-Ugly-Number-II.md diff --git a/dsa-solutions/lc-solutions/0200-0299/0263-Ugly-Number.md b/dsa-solutions/lc-solutions/0200-0299/0263-Ugly-Number.md new file mode 100644 index 000000000..1e0086816 --- /dev/null +++ b/dsa-solutions/lc-solutions/0200-0299/0263-Ugly-Number.md @@ -0,0 +1,122 @@ +--- +id: ugly-number +title: Ugly Number +sidebar_label: 0263-Ugly-Number +tags: + - Math + - Number Theory + - C++ + - Java + - Python +description: "This document provides a solution to the Ugly Number problem, where we need to determine if a number is an ugly number." +--- + +## Problem + +An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5. Given an integer `n`, return `true` if `n` is an ugly number. + +### Examples + +**Example 1:** + +Input: n = 6 +Output: true +Explanation: 6 = 2 × 3 + +**Example 2:** + +Input: n = 8 +Output: true +Explanation: 8 = 2 × 2 × 2 + +**Example 3:** + +Input: n = 14 +Output: false +Explanation: 14 is not an ugly number since it includes the prime factor 7. + +**Example 4:** + +Input: n = 1 +Output: true +Explanation: 1 is typically treated as an ugly number. + +### Constraints + +- `-2^31 <= n <= 2^31 - 1` + +### Approach + +To determine if a number is an ugly number, we can follow these steps: + +1. If `n` is less than or equal to 0, return `false` since ugly numbers are positive. +2. Divide `n` by 2, 3, and 5 as long as it is divisible by these numbers. +3. After performing the above division, if the resulting number is 1, then `n` is an ugly number. Otherwise, it is not. + +### Solution + +#### Code in Different Languages + +### C++ Solution +```cpp +#include + +using namespace std; + +bool isUgly(int n) { + if (n <= 0) return false; + while (n % 2 == 0) n /= 2; + while (n % 3 == 0) n /= 3; + while (n % 5 == 0) n /= 5; + return n == 1; +} + +int main() { + int n = 6; + cout << (isUgly(n) ? "true" : "false") << endl; +} +``` +### Java Solution + +```java +public class UglyNumber { + public static boolean isUgly(int n) { + if (n <= 0) return false; + while (n % 2 == 0) n /= 2; + while (n % 3 == 0) n /= 3; + while (n % 5 == 0) n /= 5; + return n == 1; + } + + public static void main(String[] args) { + int n = 6; + System.out.println(isUgly(n) ? "true" : "false"); + } +} +``` +### Python Solution + +```python +def isUgly(n): + if n <= 0: + return False + for i in [2, 3, 5]: + while n % i == 0: + n //= i + return n == 1 + +n = 6 +print(isUgly(n)) +``` +### Complexity Analysis +**Time Complexity:** O(logN) +>Reason: The division operations will run in logarithmic time relative to the input value n. + +**Space Complexity:** O(1) + +>Reason: We use a constant amount of extra space. + +This solution checks whether a number is an ugly number by continually dividing the number by 2, 3, and 5. If the result is 1, the number is an ugly number. The time complexity is logarithmic, and the space complexity is constant, making it efficient for large input values. + +### References +**LeetCode Problem:** Ugly Number diff --git a/dsa-solutions/lc-solutions/0200-0299/0264-Ugly-Number-II.md b/dsa-solutions/lc-solutions/0200-0299/0264-Ugly-Number-II.md new file mode 100644 index 000000000..a8c4bc87d --- /dev/null +++ b/dsa-solutions/lc-solutions/0200-0299/0264-Ugly-Number-II.md @@ -0,0 +1,175 @@ +--- +id: ugly-number-II +title: Ugly Number II +sidebar_label: 0264-Ugly-Number-II +tags: + - Math + - Number Theory + - Dynamic Programming + - Heap + - C++ + - Java + - Python +description: "This document provides a solution to the Ugly Number II problem, where we need to find the nth ugly number." +--- + +## Problem + +An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5. Given an integer `n`, return the `n`-th ugly number. + +### Examples + +**Example 1:** + +Input: n = 10 +Output: 12 +Explanation: The sequence of ugly numbers is [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ...], and the 10th ugly number is 12. + +**Example 2:** + +Input: n = 1 +Output: 1 +Explanation: 1 is typically treated as an ugly number. + +### Constraints + +- `1 <= n <= 1690` + +### Approach + +To solve this problem, we can use a dynamic programming approach to generate ugly numbers in sequence: + +1. Initialize an array `ugly_numbers` to store the first `n` ugly numbers. +2. Use three pointers (i2, i3, i5) to track the indices for multiples of 2, 3, and 5 respectively. +3. Start with the first ugly number `1` and iteratively compute the next ugly number by taking the minimum of the next multiples of 2, 3, and 5. +4. Update the corresponding pointer and move to the next position in the `ugly_numbers` array. +5. Repeat until we have generated `n` ugly numbers. + +### Solution + +#### Code in Different Languages + +### C++ Solution +```cpp +#include +#include + +using namespace std; + +int nthUglyNumber(int n) { + vector ugly_numbers(n); + ugly_numbers[0] = 1; + int i2 = 0, i3 = 0, i5 = 0; + int next_multiple_of_2 = 2; + int next_multiple_of_3 = 3; + int next_multiple_of_5 = 5; + + for (int i = 1; i < n; i++) { + int next_ugly = min(next_multiple_of_2, min(next_multiple_of_3, next_multiple_of_5)); + ugly_numbers[i] = next_ugly; + + if (next_ugly == next_multiple_of_2) { + i2++; + next_multiple_of_2 = ugly_numbers[i2] * 2; + } + if (next_ugly == next_multiple_of_3) { + i3++; + next_multiple_of_3 = ugly_numbers[i3] * 3; + } + if (next_ugly == next_multiple_of_5) { + i5++; + next_multiple_of_5 = ugly_numbers[i5] * 5; + } + } + + return ugly_numbers[n - 1]; +} + +int main() { + int n = 10; + cout << nthUglyNumber(n) << endl; // Output: 12 +} +``` +### Java Solution +```java +public class UglyNumberII { + public static int nthUglyNumber(int n) { + int[] ugly_numbers = new int[n]; + ugly_numbers[0] = 1; + int i2 = 0, i3 = 0, i5 = 0; + int next_multiple_of_2 = 2; + int next_multiple_of_3 = 3; + int next_multiple_of_5 = 5; + + for (int i = 1; i < n; i++) { + int next_ugly = Math.min(next_multiple_of_2, Math.min(next_multiple_of_3, next_multiple_of_5)); + ugly_numbers[i] = next_ugly; + + if (next_ugly == next_multiple_of_2) { + i2++; + next_multiple_of_2 = ugly_numbers[i2] * 2; + } + if (next_ugly == next_multiple_of_3) { + i3++; + next_multiple_of_3 = ugly_numbers[i3] * 3; + } + if (next_ugly == next_multiple_of_5) { + i5++; + next_multiple_of_5 = ugly_numbers[i5] * 5; + } + } + + return ugly_numbers[n - 1]; + } + + public static void main(String[] args) { + int n = 10; + System.out.println(nthUglyNumber(n)); // Output: 12 + } +} +``` +### Python Solution + +```python +def nthUglyNumber(n): + ugly_numbers = [0] * n + ugly_numbers[0] = 1 + i2 = i3 = i5 = 0 + next_multiple_of_2 = 2 + next_multiple_of_3 = 3 + next_multiple_of_5 = 5 + + for i in range(1, n): + next_ugly = min(next_multiple_of_2, next_multiple_of_3, next_multiple_of_5) + ugly_numbers[i] = next_ugly + + if next_ugly == next_multiple_of_2: + i2 += 1 + next_multiple_of_2 = ugly_numbers[i2] * 2 + if next_ugly == next_multiple_of_3: + i3 += 1 + next_multiple_of_3 = ugly_numbers[i3] * 3 + if next_ugly == next_multiple_of_5: + i5 += 1 + next_multiple_of_5 = ugly_numbers[i5] * 5 + + return ugly_numbers[n - 1] + +n = 10 +print(nthUglyNumber(n)) # Output: 12 +``` +### Complexity Analysis +**Time Complexity:** O(n) + +>Reason: We iterate through the sequence of ugly numbers up to n. + +**Space Complexity:** O(n) + +>Reason: We use an array to store the first n ugly numbers. + +This solution efficiently finds the n-th ugly number by generating the sequence of ugly numbers using a dynamic programming approach with three pointers for multiples of 2, 3, and 5. + +### References +**LeetCode Problem:** Ugly Number II + +