diff --git a/dsa-solutions/lc-solutions/0200-0299/0203-Remove-Linked-List-Elements.md b/dsa-solutions/lc-solutions/0200-0299/0203-Remove-Linked-List-Elements.md new file mode 100644 index 000000000..65697bfb3 --- /dev/null +++ b/dsa-solutions/lc-solutions/0200-0299/0203-Remove-Linked-List-Elements.md @@ -0,0 +1,176 @@ +--- +id: remove-linked-list-elements +title: Remove Linked List Elements +sidebar_label: 203-Remove-Linked-List-Elements +tags: +- Linked List +- Java +- C++ +- Python +- Data Structures +description: "This document provides solutions for removing all nodes from a linked list that have a specific value." +--- + +## Problem + +Given the head of a linked list and an integer `val`, remove all the nodes of the linked list that have `Node.val == val`, and return the new head. + +### Examples + +**Example 1:** + +**Input:** head = [1,2,6,3,4,5,6], val = 6 + +**Output:** [1,2,3,4,5] + +**Example 2:** + +**Input:** head = [], val = 1 + +**Output:** [] + +**Example 3:** + +**Input:** head = [7,7,7,7], val = 7 + +**Output:** [] + +### Constraints + +- The number of nodes in the list is in the range `[0, 10^4]`. +- `1 <= Node.val <= 50` +- `0 <= val <= 50` + +--- + +## Approach + +The approach involves iterating through the linked list and removing nodes that have the specified value. This can be achieved using a dummy node to handle edge cases where the head itself needs to be removed. + +### Steps: + +1. **Dummy Node:** + - Create a dummy node that points to the head of the list. This helps to easily handle the case where the head node itself needs to be removed. + +2. **Iterate and Remove:** + - Use a pointer to iterate through the list, checking each node's value. + - If a node's value equals `val`, adjust the pointers to bypass this node. + +3. **Return New Head:** + - Return the next node of the dummy node as the new head of the list. + +## Solution for Remove Linked List Elements + +### Java Solution + +```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 removeElements(ListNode head, int val) { + // Create a dummy node to handle edge cases + ListNode dummy = new ListNode(0); + dummy.next = head; + + // Use a pointer to iterate through the list + ListNode current = dummy; + + while (current.next != null) { + if (current.next.val == val) { + // Bypass the node with the value equal to val + current.next = current.next.next; + } else { + // Move to the next node + current = current.next; + } + } + + // Return the new head + return dummy.next; + } +} +``` +### C++ solution + +```cpp +struct ListNode { + 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* removeElements(ListNode* head, int val) { + // Create a dummy node to handle edge cases + ListNode* dummy = new ListNode(0); + dummy->next = head; + + // Use a pointer to iterate through the list + ListNode* current = dummy; + + while (current->next != nullptr) { + if (current->next->val == val) { + // Bypass the node with the value equal to val + current->next = current->next->next; + } else { + // Move to the next node + current = current->next; + } + } + + // Return the new head + return dummy->next; + } +}; +``` +### Python Solution + +```python +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +class Solution: + def removeElements(self, head: ListNode, val: int) -> ListNode: + # Create a dummy node to handle edge cases + dummy = ListNode(0) + dummy.next = head + + # Use a pointer to iterate through the list + current = dummy + + while current.next is not None: + if current.next.val == val: + # Bypass the node with the value equal to val + current.next = current.next.next + else: + # Move to the next node + current = current.next + + # Return the new head + return dummy.next +``` +### Complexity Analysis +**Time Complexity:** O(n) + +>Reason: The algorithm involves a single pass through the linked list, resulting in a time complexity of $O(n)$, where $n$ is the number of nodes in the list. + +**Space Complexity:** O(1) +>Reason: The space complexity is $O(1)$ because the algorithm uses a constant amount of extra space. + +### References +**LeetCode Problem:** Remove Linked List Elements + +**Solution Link:** Remove Linked List Elements Solution on LeetCode + + diff --git a/dsa-solutions/lc-solutions/0200-0299/0205-isomorphic-strings.md b/dsa-solutions/lc-solutions/0200-0299/0205-isomorphic-strings.md new file mode 100644 index 000000000..491f1270c --- /dev/null +++ b/dsa-solutions/lc-solutions/0200-0299/0205-isomorphic-strings.md @@ -0,0 +1,174 @@ +--- +id: isomorphic-strings +title: Isomorphic Strings +sidebar_label: 205-Isomorphic-Strings +tags: +- Hash Table +- String +- Java +- C++ +- Python +description: "This document provides solutions for determining if two strings are isomorphic." +--- + +## Problem + +Given two strings `s` and `t`, determine if they are isomorphic. + +Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`. + +All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself. + +### Examples + +**Example 1:** + +**Input:** s = "egg", t = "add" + +**Output:** true + +**Example 2:** + +**Input:** s = "foo", t = "bar" + +**Output:** false + +**Example 3:** + +**Input:** s = "paper", t = "title" + +**Output:** true + +### Constraints + +- `1 <= s.length <= 5 * 10^4` +- `t.length == s.length` +- `s` and `t` consist of any valid ASCII characters. + +--- + +## Approach + +The problem can be solved by using two hash maps to track the mappings from characters in `s` to `t` and vice versa. If the mappings are consistent throughout the strings, then the strings are isomorphic. + +### Steps: + +1. **Initialization:** + - Create two hash maps to store the character mappings from `s` to `t` and from `t` to `s`. + +2. **Iterate and Map:** + - Iterate through the characters of both strings simultaneously. + - For each character pair, check if the current mapping exists and is consistent with the previous mappings. + - If any inconsistency is found, return false. + +3. **Return Result:** + - If the iteration completes without finding any inconsistency, return true. + +## Solution for Isomorphic Strings + +### Java Solution + +```java +import java.util.HashMap; +import java.util.Map; + +class Solution { + public boolean isIsomorphic(String s, String t) { + if (s.length() != t.length()) return false; + + Map mapS = new HashMap<>(); + Map mapT = new HashMap<>(); + + for (int i = 0; i < s.length(); i++) { + char c1 = s.charAt(i); + char c2 = t.charAt(i); + + if (mapS.containsKey(c1)) { + if (mapS.get(c1) != c2) return false; + } else { + mapS.put(c1, c2); + } + + if (mapT.containsKey(c2)) { + if (mapT.get(c2) != c1) return false; + } else { + mapT.put(c2, c1); + } + } + + return true; + } +} +``` +### C++ Solution + +```cpp +#include +#include + +class Solution { +public: + bool isIsomorphic(std::string s, std::string t) { + if (s.length() != t.length()) return false; + + std::unordered_map mapS; + std::unordered_map mapT; + + for (int i = 0; i < s.length(); i++) { + char c1 = s[i]; + char c2 = t[i]; + + if (mapS.count(c1)) { + if (mapS[c1] != c2) return false; + } else { + mapS[c1] = c2; + } + + if (mapT.count(c2)) { + if (mapT[c2] != c1) return false; + } else { + mapT[c2] = c1; + } + } + + return true; + } +}; +``` +### Python Solution + +```python +class Solution: + def isIsomorphic(self, s: str, t: str) -> bool: + if len(s) != len(t): + return False + + mapS = {} + mapT = {} + + for c1, c2 in zip(s, t): + if c1 in mapS: + if mapS[c1] != c2: + return False + else: + mapS[c1] = c2 + + if c2 in mapT: + if mapT[c2] != c1: + return False + else: + mapT[c2] = c1 + + return True +``` +### Complexity Analysis +**Time Complexity:** O(n) +> Reason: The algorithm involves a single pass through both strings, resulting in a time complexity of $O(n)$, where $n$ is the length of the strings. + +**Space Complexity:** O(n) +>Reason: The space complexity is $O(n)$ due to the additional hash maps used to store the character mappings. + +### References +**LeetCode Problem:** Isomorphic Strings + +**Solution Link:** Isomorphic Strings Solution on LeetCode \ No newline at end of file