From 8952465d2de92170d708b42c44c2b6429ffa35da Mon Sep 17 00:00:00 2001 From: Hema Sree Date: Thu, 13 Jun 2024 12:15:42 +0530 Subject: [PATCH 1/3] updated --- docs/Machine Learning/An-Introduction -to-Machine-Learning.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Machine Learning/An-Introduction -to-Machine-Learning.md b/docs/Machine Learning/An-Introduction -to-Machine-Learning.md index f51c486c5..99a77a3c1 100644 --- a/docs/Machine Learning/An-Introduction -to-Machine-Learning.md +++ b/docs/Machine Learning/An-Introduction -to-Machine-Learning.md @@ -1,5 +1,5 @@ --- -id: Machine Learning +id: machine-learning title: Introduction to Machine Learning sidebar_label: An Introduction to Machine Learning sidebar_position: 8 From b6dffb4fd87465be8e21f722072fc296e87c6af1 Mon Sep 17 00:00:00 2001 From: Hema Sree Date: Thu, 13 Jun 2024 18:57:46 +0530 Subject: [PATCH 2/3] Added 91 --- .../0000-0099/0091-Decode-Ways.md | 206 ++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0000-0099/0091-Decode-Ways.md diff --git a/dsa-solutions/lc-solutions/0000-0099/0091-Decode-Ways.md b/dsa-solutions/lc-solutions/0000-0099/0091-Decode-Ways.md new file mode 100644 index 000000000..7fcd9a99a --- /dev/null +++ b/dsa-solutions/lc-solutions/0000-0099/0091-Decode-Ways.md @@ -0,0 +1,206 @@ +--- +id: decode-ways +title: Decode Ways +sidebar_label: 0091 - Decode Ways +tags: + - DP + - Leetcode + +description: "This is a solution to the Decode Ways on LeetCode." +--- + +## Problem Statement + +A message containing letters from A-Z can be encoded into numbers using the following mapping: + +``` +'A' -> "1" +'B' -> "2" +... +'Z' -> "26" +``` + +To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, "11106" can be mapped into: + +"AAJF" with the grouping (1 1 10 6) +"KJF" with the grouping (11 10 6) +Note that the grouping (1 11 06) is invalid because "06" cannot be mapped into 'F' since "6" is different from "06". + +Given a string s containing only digits, return the number of ways to decode it. + +The test cases are generated so that the answer fits in a 32-bit integer. + +### Examples + +**Example 1:** + +``` +Input: s = "12" +Output: 2 +Explanation: "12" could be decoded as "AB" (1 2) or "L" (12). +``` + +**Example 2:** + +``` +Input: s = "226" +Output: 3 +Explanation: "226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6). +``` + +**Example 3:** + +``` +Input: s = "06" +Output: 0 +Explanation: "06" cannot be mapped to "F" because of the leading zero ("6" is different from "06"). +``` + +Constraints: + +- $1 <= s.length <= 100$ +- s contains only digits and may contain leading zero(s). + +### Algorithm + +1. If the string `s` is empty or starts with '0', return 0 as it cannot be decoded. +2. Initialize a `dp` array where `dp[i]` represents the number of ways to decode the substring `s[0:i]`. +3. Set `dp[0]` to 1 (base case for the empty string). +4. Set `dp[1]` based on the first character of the string (1 if the first character is not '0', otherwise 0). +5. Iterate through the string from the second character to the end: + - For each character, check if it forms a valid single-digit number (between '1' and '9'). If it does, add `dp[i-1]` to `dp[i]`. + - Check if the two-digit number formed with the previous character is valid (between "10" and "26"). If it does, add `dp[i-2]` to `dp[i]`. +6. The result is in `dp[n]`, where `n` is the length of the string. + +### Pseudocode + +``` +function decode(s): + if s is empty or s[0] is '0': + return 0 + + n = length of s + dp = array of size (n + 1) initialized to 0 + dp[0] = 1 + dp[1] = 1 if s[0] != '0' else 0 + + for i from 2 to n: + if s[i-1] != '0': + dp[i] += dp[i-1] + + two_digit = integer value of s[i-2:i] + if 10 <= two_digit <= 26: + dp[i] += dp[i-2] + + return dp[n] +``` + +### Python + +```python +class Solution: + def decode(self, s: str) -> int: + if not s or s[0] == '0': + return 0 + + n = len(s) + dp = [0] * (n + 1) + dp[0] = 1 + dp[1] = 1 if s[0] != '0' else 0 + + for i in range(2, n + 1): + if s[i - 1] != '0': + dp[i] += dp[i - 1] + + two_digit = int(s[i - 2:i]) + if 10 <= two_digit <= 26: + dp[i] += dp[i - 2] + + return dp[n] +``` + +### C++ + +```cpp +class Solution { +public: + int decode(string s) { + if (s.empty() || s[0] == '0') return 0; + + int n = s.size(); + vector dp(n + 1, 0); + dp[0] = 1; + dp[1] = s[0] != '0' ? 1 : 0; + + for (int i = 2; i <= n; ++i) { + if (s[i - 1] != '0') { + dp[i] += dp[i - 1]; + } + + int two_digit = stoi(s.substr(i - 2, 2)); + if (10 <= two_digit && two_digit <= 26) { + dp[i] += dp[i - 2]; + } + } + + return dp[n]; + } +}; + +``` + +### Java + +```java +class Solution { + public int decode(String s) { + if (s == null || s.length() == 0 || s.charAt(0) == '0') { + return 0; + } + + int n = s.length(); + int[] dp = new int[n + 1]; + dp[0] = 1; + dp[1] = s.charAt(0) != '0' ? 1 : 0; + + for (int i = 2; i <= n; ++i) { + if (s.charAt(i - 1) != '0') { + dp[i] += dp[i - 1]; + } + + int twoDigit = Integer.parseInt(s.substring(i - 2, i)); + if (10 <= twoDigit && twoDigit <= 26) { + dp[i] += dp[i - 2]; + } + } + + return dp[n]; + } +} +``` + +### JavaScript + +```javascript +var decode = function (s) { + if (!s || s[0] === "0") return 0; + + let n = s.length; + let dp = new Array(n + 1).fill(0); + dp[0] = 1; + dp[1] = s[0] !== "0" ? 1 : 0; + + for (let i = 2; i <= n; i++) { + if (s[i - 1] !== "0") { + dp[i] += dp[i - 1]; + } + + let twoDigit = parseInt(s.substring(i - 2, i), 10); + if (10 <= twoDigit && twoDigit <= 26) { + dp[i] += dp[i - 2]; + } + } + + return dp[n]; +}; +``` From 33efd095b4b2c473d638e7091b37d448bdcaaf2b Mon Sep 17 00:00:00 2001 From: Hema Sree Date: Thu, 13 Jun 2024 20:03:18 +0530 Subject: [PATCH 3/3] Added 92 --- .../0000-0099/0091-Decode-Ways.md | 2 +- .../0000-0099/0092-Reverse-LinkedList-II.md | 241 ++++++++++++++++++ 2 files changed, 242 insertions(+), 1 deletion(-) create mode 100644 dsa-solutions/lc-solutions/0000-0099/0092-Reverse-LinkedList-II.md diff --git a/dsa-solutions/lc-solutions/0000-0099/0091-Decode-Ways.md b/dsa-solutions/lc-solutions/0000-0099/0091-Decode-Ways.md index 7fcd9a99a..2fcddbbf7 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0091-Decode-Ways.md +++ b/dsa-solutions/lc-solutions/0000-0099/0091-Decode-Ways.md @@ -56,7 +56,7 @@ Output: 0 Explanation: "06" cannot be mapped to "F" because of the leading zero ("6" is different from "06"). ``` -Constraints: +### Constraints: - $1 <= s.length <= 100$ - s contains only digits and may contain leading zero(s). diff --git a/dsa-solutions/lc-solutions/0000-0099/0092-Reverse-LinkedList-II.md b/dsa-solutions/lc-solutions/0000-0099/0092-Reverse-LinkedList-II.md new file mode 100644 index 000000000..f56a33f8c --- /dev/null +++ b/dsa-solutions/lc-solutions/0000-0099/0092-Reverse-LinkedList-II.md @@ -0,0 +1,241 @@ +--- +id: reverse-linked-list-II +title: Reverse Linked List II +sidebar_label: 0092 - Reverse Linked List II +tags: + - Linked List + - Leetcode +description: "This is a solution to the Reverse Linked List II problem on LeetCode." +--- + +## Problem Statement + +Given the head of a singly linked list and two integers left and right where `left <= right`, reverse the nodes of the list from position left to position right, and return the reversed list. + +### Examples + +**Example 1:** + +``` +Input: head = [1,2,3,4,5], left = 2, right = 4 +Output: [1,4,3,2,5] +``` + +**Example 2:** + +``` +Input: head = [5], left = 1, right = 1 +Output: [5] +``` + +### Constraints: + +- The number of nodes in the list is `n`. +- $1 <= n <= 500$ +- $-500 <= Node.val <= 500$ +- $1 <= left <= right <= n$ + +### Algorithm + +1. Create a dummy node pointing to the head of the list to handle edge cases easily. +2. Find the node just before the `left` position (`leftNode`) and the node at the `left` position (`curr`). +3. Reverse the sublist from `left` to `right`. +4. Reconnect the reversed sublist back to the list. +5. Return the new head of the list (`dummy.next`). + +### Pseudocode + +``` +function reverseLinkedList(head, left, right): + if head is null: + return null + + dummy = new ListNode(0) + dummy.next = head + leftNode = dummy + curr = head + + for i from 0 to left-2: + leftNode = leftNode.next + curr = curr.next + + subhead = curr + preNode = null + + for i from 0 to right-left: + nextNode = curr.next + curr.next = preNode + preNode = curr + curr = nextNode + + leftNode.next = preNode + subhead.next = curr + + return dummy.next +``` + +### Python + +```python +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +class Solution: + def reverseLinkedList(self, head: ListNode, left: int, right: int) -> ListNode: + if not head: + return None + + dummy = ListNode(0) + dummy.next = head + leftNode = dummy + curr = head + + for i in range(left - 1): + leftNode = leftNode.next + curr = curr.next + + subhead = curr + preNode = None + + for i in range(right - left + 1): + nextNode = curr.next + curr.next = preNode + preNode = curr + curr = nextNode + + leftNode.next = preNode + subhead.next = curr + + return dummy.next +``` + +### Java + +```java +class ListNode { + int val; + ListNode next; + ListNode() {} + ListNode(int val) { this.val = val; } + ListNode(int val, ListNode next) { this.val = val; this.next = next; } +} + +class Solution { + public ListNode reverseLinkedList(ListNode head, int left, int right) { + if (head == null) return null; + + ListNode dummy = new ListNode(0); + dummy.next = head; + ListNode leftNode = dummy; + ListNode curr = head; + + for (int i = 0; i < left - 1; i++) { + leftNode = leftNode.next; + curr = curr.next; + } + + ListNode subhead = curr; + ListNode preNode = null; + + for (int i = 0; i <= right - left; i++) { + ListNode nextNode = curr.next; + curr.next = preNode; + preNode = curr; + curr = nextNode; + } + + leftNode.next = preNode; + subhead.next = curr; + + return dummy.next; + } +} +``` + +### C++ + +```cpp +class ListNode { +public: + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; + +class Solution { +public: + ListNode* reverseLinkedList(ListNode* head, int left, int right) { + if (!head) return nullptr; + + ListNode* dummy = new ListNode(0); + dummy->next = head; + + ListNode* leftNode = dummy; + ListNode* curr = head; + + for (int i = 0; i < left - 1; i++) { + leftNode = leftNode->next; + curr = curr->next; + } + + ListNode* subhead = curr; + ListNode* preNode = nullptr; + + for (int i = 0; i <= right - left; i++) { + ListNode* nextNode = curr->next; + curr->next = preNode; + preNode = curr; + curr = nextNode; + } + + leftNode->next = preNode; + subhead->next = curr; + + return dummy->next; + } +}; +``` + +### JavaScript + +```javascript +class ListNode { + constructor(val = 0, next = null) { + this.val = val; + this.next = next; + } +} + +var reverseLinkedList = function (head, left, right) { + if (!head) return null; + + let dummy = new ListNode(0); + dummy.next = head; + let leftNode = dummy; + let curr = head; + + for (let i = 0; i < left - 1; i++) { + leftNode = leftNode.next; + curr = curr.next; + } + + let subhead = curr; + let preNode = null; + + for (let i = 0; i <= right - left; i++) { + let nextNode = curr.next; + curr.next = preNode; + preNode = curr; + curr = nextNode; + } + + leftNode.next = preNode; + subhead.next = curr; + + return dummy.next; +}; +```