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 76a29c9c4..26cf596b7 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` @@ -48,11 +47,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. @@ -62,7 +59,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:** @@ -157,7 +153,6 @@ public class Solution { ``` #### CPP: - ```cpp #include #include @@ -213,41 +208,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: @@ -255,40 +249,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: @@ -301,11 +294,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. @@ -314,7 +305,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:** @@ -322,4 +312,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 diff --git a/dsa-solutions/lc-solutions/0200-0299/0237-Delete-Node-in-a-Linked-List.md b/dsa-solutions/lc-solutions/0200-0299/0237-Delete-Node-in-a-Linked-List.md new file mode 100644 index 000000000..1649d9b06 --- /dev/null +++ b/dsa-solutions/lc-solutions/0200-0299/0237-Delete-Node-in-a-Linked-List.md @@ -0,0 +1,170 @@ +--- +id: delete-node-in-a-linked-list +title: Delete Node in a Linked List +sidebar_label: 0237-Delete-Node-in-a-Linked-List +tags: + - Linked List + - C++ + - Java + - Python +description: "This document provides a solution to the Delete Node in a Linked List problem, where we need to delete a given node from a singly linked list." +--- + +## Problem + +Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly. + +It is guaranteed that the node to be deleted is not a tail node in the list. + +All the values of the linked list are unique, and it is guaranteed that the given node node is not the last node in the linked list. + +Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean: + +- The value of the given node should not exist in the linked list. +- The number of nodes in the linked list should decrease by one. +- All the values before node should be in the same order. +- All the values after node should be in the same order. + +### Custom testing: + +- For the input, you should provide the entire linked list head and the node to be given node. node should not be the last node of the list and should be an actual node in the list. +- We will build the linked list and pass the node to your function. +- The output will be the entire list after calling your function. + +### Examples + +**Example 1:** + +Input: head = [4,5,1,9], node = 5 +Output: [4,1,9] +Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function. + +**Example 2:** + +Input: head = [4,5,1,9], node = 1 +Output: [4,5,9] +Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function. + +### Constraints + +- The number of nodes in the list is in the range `[2, 1000]`. +- `-1000 <= Node.val <= 1000` +- The value of each node in the list is unique. +- The `node` to be deleted is in the list and is not a tail node. + +### Approach + +Since we do not have access to the head of the list, we cannot use the standard deletion method. Instead, we can solve the problem by copying the value of the next node to the current node and then deleting the next node. This effectively removes the node from the list. + +### Solution + +#### Code in Different Languages + +### C++ Solution +```cpp +#include + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + +void deleteNode(ListNode* node) { + if (node == nullptr || node->next == nullptr) return; + ListNode* next_node = node->next; + node->val = next_node->val; + node->next = next_node->next; + delete next_node; +} + +int main() { + ListNode* head = new ListNode(4); + head->next = new ListNode(5); + head->next->next = new ListNode(1); + head->next->next->next = new ListNode(9); + + deleteNode(head->next); // Deletes node with value 5 + + ListNode* current = head; + while (current) { + std::cout << current->val << " "; + current = current->next; + } + std::cout << std::endl; +} +``` +### Java Solution + +```java +class ListNode { + int val; + ListNode next; + ListNode(int x) { val = x; } +} + +public class DeleteNode { + public void deleteNode(ListNode node) { + if (node == null || node.next == null) return; + node.val = node.next.val; + node.next = node.next.next; + } + + public static void main(String[] args) { + ListNode head = new ListNode(4); + head.next = new ListNode(5); + head.next.next = new ListNode(1); + head.next.next.next = new ListNode(9); + + new DeleteNode().deleteNode(head.next); // Deletes node with value 5 + + ListNode current = head; + while (current != null) { + System.out.print(current.val + " "); + current = current.next; + } + System.out.println(); + } +} +``` +### Python Solution + +```python +class ListNode: + def __init__(self, x): + self.val = x + self.next = None + +def deleteNode(node): + if not node or not node.next: + return + node.val = node.next.val + node.next = node.next.next + +# Test +head = ListNode(4) +head.next = ListNode(5) +head.next.next = ListNode(1) +head.next.next.next = ListNode(9) + +deleteNode(head.next) # Deletes node with value 5 + +current = head +while current: + print(current.val, end=" ") + current = current.next +print() +``` +### Complexity Analysis +**Time Complexity:** O(1) + +>Reason: The operation involves copying the value and changing pointers, which are constant time operations. + +**Space Complexity:** O(1) + +>Reason: We use a constant amount of extra space. + +This solution deletes a node in a singly-linked list by copying the value of the next node to the current node and adjusting the pointers accordingly, achieving the desired result with constant time and space complexity. + +### References +**LeetCode Problem:** Delete Node in a Linked List \ No newline at end of file