From 424bee9d2c230d85dd51e3959e2893308be7adf9 Mon Sep 17 00:00:00 2001 From: Matvii Hodovaniuk Date: Mon, 27 Jul 2026 16:34:13 +0200 Subject: [PATCH 1/2] Create 2026-07-27-leetcoding-0.md --- _posts/2026-07-27-leetcoding-0.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 _posts/2026-07-27-leetcoding-0.md diff --git a/_posts/2026-07-27-leetcoding-0.md b/_posts/2026-07-27-leetcoding-0.md new file mode 100644 index 0000000..dc0e15a --- /dev/null +++ b/_posts/2026-07-27-leetcoding-0.md @@ -0,0 +1,20 @@ +# Daily LeetCode Practice & Key Learnings + +## [217. Contains Duplicate](https://leetcode.com/problems/contains-duplicate/description/) +- **Data Structure Choice:** Use a `Set` for $O(1)$ average-time lookups to trade a small amount of auxiliary space for speed. +- **Early Exit:** Checking `set.has(num)` inside an iterative loop allows an immediate early exit on the first duplicate, whereas `new Set(nums).size !== nums.length` forces full $O(N)$ allocation and execution every time. +- **Clean Syntax:** Prefer `for (const num of nums)` over indexed `for (let i = 0...)` loops when array indices are not explicitly required. + +## [242. Valid Anagram](https://leetcode.com/problems/valid-anagram/description/) +- **Loop Bounds Safety:** Always verify loop boundaries match the target data structure. Iterating up to `s.length` instead of the fixed frequency array size (26) leads to array-out-of-bounds checks or false positives. +- **Space Optimization:** A single 26-element array can be used instead of two separate counter arrays by incrementing counts for string `s` and decrementing for string `t`. +- **Unicode Flexibility:** Fixed-size arrays (26 buckets) only work for ASCII lowercase letters. For full UTF-8/emoji support, transition to a `Map`. + +## [49. Group Anagrams](https://leetcode.com/problems/group-anagrams/description/) +- **Hashing Strategy:** Character frequency arrays can be converted to unique string keys (e.g., `"1#0#2..."`) to group anagrams in $O(N \cdot K)$ time without sorting strings $(O(N \cdot K \log K))$. +- **In-Place Mutation:** Avoid spreading arrays inside loops (`[...oldValue, item]`), as shallow copying creates an $O(M)$ overhead per insert. Using `map.get().push(item)` directly mutates in $O(1)$ amortized time. +- **Complexity Analysis:** Separate string count ($N$) from individual string length ($K$). Total runtime is $O(N \cdot K)$, not $O(N^2)$. + +## [125. Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/) +- **Two-Pointer Pattern:** Moving `left` and `right` pointers toward the center allows in-place $O(1)$ space verification without making modified string copies. +- **Inner Loop Safety:** When using inner `while` loops to skip non-alphanumeric characters, always include boundary guards (`left < right`) to prevent infinite loops (TLE) or out-of-bounds pointer movement. From 5d48bb74d86bf1b8f25bd8763fd9d9c31a15eb0a Mon Sep 17 00:00:00 2001 From: Matvii Hodovaniuk Date: Mon, 27 Jul 2026 16:35:46 +0200 Subject: [PATCH 2/2] Update 2026-07-27-leetcoding-0.md --- _posts/2026-07-27-leetcoding-0.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/_posts/2026-07-27-leetcoding-0.md b/_posts/2026-07-27-leetcoding-0.md index dc0e15a..5c59f8f 100644 --- a/_posts/2026-07-27-leetcoding-0.md +++ b/_posts/2026-07-27-leetcoding-0.md @@ -1,20 +1,26 @@ # Daily LeetCode Practice & Key Learnings -## [217. Contains Duplicate](https://leetcode.com/problems/contains-duplicate/description/) +## [217. Contains Duplicate](https://leetcode.com/problems/contains-duplicate/description/) | [Watch Video](https://youtu.be/2o0AINvxawg) - **Data Structure Choice:** Use a `Set` for $O(1)$ average-time lookups to trade a small amount of auxiliary space for speed. - **Early Exit:** Checking `set.has(num)` inside an iterative loop allows an immediate early exit on the first duplicate, whereas `new Set(nums).size !== nums.length` forces full $O(N)$ allocation and execution every time. - **Clean Syntax:** Prefer `for (const num of nums)` over indexed `for (let i = 0...)` loops when array indices are not explicitly required. -## [242. Valid Anagram](https://leetcode.com/problems/valid-anagram/description/) +--- + +## [242. Valid Anagram](https://leetcode.com/problems/valid-anagram/description/) | [Watch Video](https://youtu.be/6GsBIM84lKM) - **Loop Bounds Safety:** Always verify loop boundaries match the target data structure. Iterating up to `s.length` instead of the fixed frequency array size (26) leads to array-out-of-bounds checks or false positives. - **Space Optimization:** A single 26-element array can be used instead of two separate counter arrays by incrementing counts for string `s` and decrementing for string `t`. - **Unicode Flexibility:** Fixed-size arrays (26 buckets) only work for ASCII lowercase letters. For full UTF-8/emoji support, transition to a `Map`. -## [49. Group Anagrams](https://leetcode.com/problems/group-anagrams/description/) +--- + +## [49. Group Anagrams](https://leetcode.com/problems/group-anagrams/description/) | [Watch Video](https://youtu.be/QRueEXHt6z4) - **Hashing Strategy:** Character frequency arrays can be converted to unique string keys (e.g., `"1#0#2..."`) to group anagrams in $O(N \cdot K)$ time without sorting strings $(O(N \cdot K \log K))$. - **In-Place Mutation:** Avoid spreading arrays inside loops (`[...oldValue, item]`), as shallow copying creates an $O(M)$ overhead per insert. Using `map.get().push(item)` directly mutates in $O(1)$ amortized time. - **Complexity Analysis:** Separate string count ($N$) from individual string length ($K$). Total runtime is $O(N \cdot K)$, not $O(N^2)$. -## [125. Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/) +--- + +## [125. Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/) | [Watch Video](https://youtu.be/kmXiYU0fdlI) - **Two-Pointer Pattern:** Moving `left` and `right` pointers toward the center allows in-place $O(1)$ space verification without making modified string copies. - **Inner Loop Safety:** When using inner `while` loops to skip non-alphanumeric characters, always include boundary guards (`left < right`) to prevent infinite loops (TLE) or out-of-bounds pointer movement.