From accb9f5ad3e521adf76f1e5f334e0e89f8326b84 Mon Sep 17 00:00:00 2001 From: Hema Sree Date: Sun, 23 Jun 2024 17:51:49 +0530 Subject: [PATCH 1/3] added 383 --- .../0300-0399/0383-ransom-note.md | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0300-0399/0383-ransom-note.md diff --git a/dsa-solutions/lc-solutions/0300-0399/0383-ransom-note.md b/dsa-solutions/lc-solutions/0300-0399/0383-ransom-note.md new file mode 100644 index 000000000..753b7f35d --- /dev/null +++ b/dsa-solutions/lc-solutions/0300-0399/0383-ransom-note.md @@ -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 + +class Solution { +public: + bool canConstruct(std::string ransomNote, std::string magazine) { + std::unordered_map 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 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; +} +``` From 0838fd287b6f2e69f788e3499fc0c03aa064a464 Mon Sep 17 00:00:00 2001 From: Hema Sree Date: Sun, 23 Jun 2024 18:01:28 +0530 Subject: [PATCH 2/3] Added 389 --- .../0300-0399/0389-find-the-difference.md | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0300-0399/0389-find-the-difference.md diff --git a/dsa-solutions/lc-solutions/0300-0399/0389-find-the-difference.md b/dsa-solutions/lc-solutions/0300-0399/0389-find-the-difference.md new file mode 100644 index 000000000..a8b8d11ed --- /dev/null +++ b/dsa-solutions/lc-solutions/0300-0399/0389-find-the-difference.md @@ -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 + +char findTheDifference(std::string s, std::string t) { + std::unordered_map 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 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 +} +``` From 4bc45cb02fab72588791820138f3b85e0f6672a2 Mon Sep 17 00:00:00 2001 From: Hema Sree Date: Sun, 23 Jun 2024 18:05:58 +0530 Subject: [PATCH 3/3] change --- .../lc-solutions/0300-0399/0389-find-the-difference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsa-solutions/lc-solutions/0300-0399/0389-find-the-difference.md b/dsa-solutions/lc-solutions/0300-0399/0389-find-the-difference.md index a8b8d11ed..b805f8198 100644 --- a/dsa-solutions/lc-solutions/0300-0399/0389-find-the-difference.md +++ b/dsa-solutions/lc-solutions/0300-0399/0389-find-the-difference.md @@ -1,5 +1,5 @@ --- -id: Find the Difference +id: find-the-difference title: Find the Difference sidebar_label: 0389-Find the Difference tags: