Skip to content

Added 389 #1922

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 23, 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
133 changes: 133 additions & 0 deletions dsa-solutions/lc-solutions/0300-0399/0383-ransom-note.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
id: ransome-note
title: Ransom Note
sidebar_label: 0383-Ransom Note
tags:
- Leet code
description: "Solution to leetocde 383"
---

### Problem Description

Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.

Each letter in magazine can only be used once in ransomNote.

### Examples

Example 1:

```
Input: ransomNote = "a", magazine = "b"
Output: false
```

Example 2:

```
Input: ransomNote = "aa", magazine = "ab"
Output: false
```

Example 3:

```
Input: ransomNote = "aa", magazine = "aab"
Output: true
```

### Constraints:

- $1 <= ransomNote.length, magazine.length <= 10^5$
- ransomNote and magazine consist of lowercase English letters.
Alright, here's the breakdown of the code you provided, along with implementations in various languages:

### Algorithm:

1. **Initialize a frequency table:** The code creates an array `alpha` of size 26. This array will store the frequency of each letter in the magazine (`a` maps to index 0, `b` to index 1, and so on).
2. **Iterate through ransom note:** For each character `c` in the ransom note:
- Calculate the index `idx` using `ord(c) - ord('a')`. This converts the character `c` to its corresponding numerical position (e.g., 'a' becomes 0, 'b' becomes 1).
- Use `magazine.find(c, alpha[idx])` to find the first occurrence of `c` in the magazine starting from index `alpha[idx]`. This improves efficiency by avoiding redundant searches in the magazine.
- If `i` (the result of `find`) is -1, it means the character `c` is not found in the magazine. In this case, the function returns `False` as the ransom note cannot be formed.
- Update `alpha[idx]` with `i + 1` to track the last position where `c` was found in the magazine. This helps avoid revisiting the same characters in the magazine unnecessarily.
3. **Return True:** After iterating through the entire ransom note, if no characters were missing, the function returns `True`, indicating that the ransom note can be constructed from the magazine.

**Code Implementations:**

**Python:**

```python
from collections import Counter

class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
magazine_counter = Counter(magazine)
ransom_counter = Counter(ransomNote)
for char, count in ransom_counter.items():
if char not in magazine_counter or magazine_counter[char] < count:
return False
return True
```

**C++:**

```c++
#include <unordered_map>

class Solution {
public:
bool canConstruct(std::string ransomNote, std::string magazine) {
std::unordered_map<char, int> magazine_counter;
for (char c : magazine) {
magazine_counter[c]++;
}
for (char c : ransomNote) {
if (magazine_counter.find(c) == magazine_counter.end() || magazine_counter[c] == 0) {
return false;
}
magazine_counter[c]--;
}
return true;
}
};
```

**Java:**

```java
import java.util.HashMap;

class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
HashMap<Character, Integer> magazine_counter = new HashMap<>();
for (char c : magazine.toCharArray()) {
magazine_counter.put(c, magazine_counter.getOrDefault(c, 0) + 1);
}
for (char c : ransomNote.toCharArray()) {
if (!magazine_counter.containsKey(c) || magazine_counter.get(c) == 0) {
return false;
}
magazine_counter.put(c, magazine_counter.get(c) - 1);
}
return true;
}
}
```

**JavaScript:**

```javascript
function canConstruct(ransomNote, magazine) {
const magazine_counter = {};
for (const char of magazine) {
magazine_counter[char] = (magazine_counter[char] || 0) + 1;
}
for (const char of ransomNote) {
if (!(char in magazine_counter) || magazine_counter[char] === 0) {
return false;
}
magazine_counter[char]--;
}
return true;
}
```
111 changes: 111 additions & 0 deletions dsa-solutions/lc-solutions/0300-0399/0389-find-the-difference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
---
id: find-the-difference
title: Find the Difference
sidebar_label: 0389-Find the Difference
tags:
- Leet code
description: "Solution to leetocde 389"
---

### Problem Description

You are given two strings s and t.

String t is generated by random shuffling string s and then add one more letter at a random position.

Return the letter that was added to t.

Example 1:

```
Input: s = "abcd", t = "abcde"
Output: "e"
Explanation: 'e' is the letter that was added.
```

Example 2:

```
Input: s = "", t = "y"
Output: "y"
```

### Constraints:

- $0 <= s.length <= 1000$
- $t.length == s.length + 1$
- $s and t consist of lowercase English letters.$

### Algorithm

1. For each character i in t, it checks if the count of i in t is greater than the count of i in s. If this condition is met, it means that i is the extra letter that was added to t, so it returns i and breaks out of the loop.

### Code Implementation

**Python:**

```python
class Solution(object):
def findTheDifference(self, s, t):
for char in t:
if t.count(char) > s.count(char):
return char
```

**C++:**

```c++
#include <unordered_map>

char findTheDifference(std::string s, std::string t) {
std::unordered_map<char, int> char_counts;
for (char c : s) {
char_counts[c]++;
}
for (char c : t) {
if (char_counts.find(c) == char_counts.end() || char_counts[c] == 0) {
return c;
}
char_counts[c]--;
}
return '\0'; // Return null character if no difference found
}
```

**Java:**

```java
public class Solution {
public char findTheDifference(String s, String t) {
HashMap<Character, Integer> char_counts = new HashMap<>();
for (char c : s.toCharArray()) {
char_counts.put(c, char_counts.getOrDefault(c, 0) + 1);
}
for (char c : t.toCharArray()) {
if (!char_counts.containsKey(c) || char_counts.get(c) == 0) {
return c;
}
char_counts.put(c, char_counts.get(c) - 1);
}
return '\u0000'; // Return null character if no difference found (Unicode representation)
}
}
```

**JavaScript:**

```javascript
function findTheDifference(s, t) {
const char_counts = {};
for (const char of s) {
char_counts[char] = (char_counts[char] || 0) + 1;
}
for (const char of t) {
if (!(char in char_counts) || char_counts[char] === 0) {
return char;
}
char_counts[char]--;
}
return ""; // Return empty string if no difference found
}
```
Loading