Skip to content

[Feature Request]: Leetcode:203 & 205 #986 #1041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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


174 changes: 174 additions & 0 deletions dsa-solutions/lc-solutions/0200-0299/0205-isomorphic-strings.md
Original file line number Diff line number Diff line change
@@ -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<Character, Character> mapS = new HashMap<>();
Map<Character, Character> 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 <unordered_map>
#include <string>

class Solution {
public:
bool isIsomorphic(std::string s, std::string t) {
if (s.length() != t.length()) return false;

std::unordered_map<char, char> mapS;
std::unordered_map<char, char> 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
Loading