Skip to content

Commit c321ad1

Browse files
committed
feat: add more problems
1 parent 83247bb commit c321ad1

File tree

9 files changed

+271
-3
lines changed

9 files changed

+271
-3
lines changed

.templates/leetcode/json/kth_smallest_element_in_a_bst.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"problem_title": "Kth Smallest Element in a BST",
66
"difficulty": "Medium",
77
"topics": "Tree, Depth-First Search, Binary Search Tree, Binary Tree",
8-
"tags": [],
8+
"tags": ["grind-75"],
99
"readme_description": "Given the `root` of a binary search tree, and an integer `k`, return the `k`th smallest value (1-indexed) of all the values of the nodes in the tree.",
1010
"readme_examples": [
1111
{
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"problem_name": "minimum_window_substring",
3+
"solution_class_name": "Solution",
4+
"problem_number": "76",
5+
"problem_title": "Minimum Window Substring",
6+
"difficulty": "Hard",
7+
"topics": "Hash Table, String, Sliding Window",
8+
"tags": ["grind-75", "neetcode-150"],
9+
"readme_description": "Given two strings `s` and `t` of lengths `m` and `n` respectively, return the **minimum window substring** of `s` such that every character in `t` (including duplicates) is included in the window. If there is no such substring, return the empty string `\"\"`.\n\nThe testcases will be generated such that the answer is unique.",
10+
"readme_examples": [
11+
{
12+
"content": "```\nInput: s = \"ADOBECODEBANC\", t = \"ABC\"\nOutput: \"BANC\"\n```\n**Explanation:** The minimum window substring \"BANC\" includes 'A', 'B', and 'C' from string t."
13+
},
14+
{
15+
"content": "```\nInput: s = \"a\", t = \"a\"\nOutput: \"a\"\n```\n**Explanation:** The entire string s is the minimum window."
16+
},
17+
{
18+
"content": "```\nInput: s = \"a\", t = \"aa\"\nOutput: \"\"\n```\n**Explanation:** Both 'a's from t must be included in the window. Since the largest window of s only has one 'a', return empty string."
19+
}
20+
],
21+
"readme_constraints": "- `m == s.length`\n- `n == t.length`\n- `1 <= m, n <= 10^5`\n- `s` and `t` consist of uppercase and lowercase English letters.",
22+
"readme_additional": "**Follow up:** Could you find an algorithm that runs in `O(m + n)` time?",
23+
"solution_imports": "",
24+
"solution_methods": [
25+
{
26+
"name": "min_window",
27+
"parameters": "s: str, t: str",
28+
"return_type": "str",
29+
"dummy_return": "\"\""
30+
}
31+
],
32+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
33+
"test_class_name": "MinimumWindowSubstring",
34+
"test_helper_methods": [
35+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
36+
],
37+
"test_methods": [
38+
{
39+
"name": "test_min_window",
40+
"parametrize": "s, t, expected",
41+
"parametrize_typed": "s: str, t: str, expected: str",
42+
"test_cases": "[(\"ADOBECODEBANC\", \"ABC\", \"BANC\"), (\"a\", \"a\", \"a\"), (\"a\", \"aa\", \"\")]",
43+
"body": "result = self.solution.min_window(s, t)\nassert result == expected"
44+
}
45+
],
46+
"playground_imports": "from solution import Solution",
47+
"playground_test_case": "# Example test case\ns = \\\"ADOBECODEBANC\\\"\nt = \\\"ABC\\\"\nexpected = \\\"BANC\\\"",
48+
"playground_execution": "result = Solution().min_window(s, t)\nresult",
49+
"playground_assertion": "assert result == expected"
50+
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PYTHON_VERSION = 3.13
2-
PROBLEM ?= kth_smallest_element_in_a_bst
2+
PROBLEM ?= minimum_window_substring
33
FORCE ?= 0
44

55
sync_submodules:

leetcode/kth_smallest_element_in_a_bst/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
**Difficulty:** Medium
44
**Topics:** Tree, Depth-First Search, Binary Search Tree, Binary Tree
5-
**Tags:**
5+
**Tags:** grind-75
66

77
**LeetCode:** [Problem 230](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/)
88

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Minimum Window Substring
2+
3+
**Difficulty:** Hard
4+
**Topics:** Hash Table, String, Sliding Window
5+
**Tags:** grind-75, neetcode-150
6+
7+
**LeetCode:** [Problem 76](https://leetcode.com/problems/minimum-window-substring/description/)
8+
9+
## Problem Description
10+
11+
Given two strings `s` and `t` of lengths `m` and `n` respectively, return the **minimum window substring** of `s` such that every character in `t` (including duplicates) is included in the window. If there is no such substring, return the empty string `""`.
12+
13+
The testcases will be generated such that the answer is unique.
14+
15+
## Examples
16+
17+
### Example 1:
18+
19+
```
20+
Input: s = "ADOBECODEBANC", t = "ABC"
21+
Output: "BANC"
22+
```
23+
24+
**Explanation:** The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
25+
26+
### Example 2:
27+
28+
```
29+
Input: s = "a", t = "a"
30+
Output: "a"
31+
```
32+
33+
**Explanation:** The entire string s is the minimum window.
34+
35+
### Example 3:
36+
37+
```
38+
Input: s = "a", t = "aa"
39+
Output: ""
40+
```
41+
42+
**Explanation:** Both 'a's from t must be included in the window. Since the largest window of s only has one 'a', return empty string.
43+
44+
## Constraints
45+
46+
- `m == s.length`
47+
- `n == t.length`
48+
- `1 <= m, n <= 10^5`
49+
- `s` and `t` consist of uppercase and lowercase English letters.
50+
51+
**Follow up:** Could you find an algorithm that runs in `O(m + n)` time?

leetcode/minimum_window_substring/__init__.py

Whitespace-only changes.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "imports",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from solution import Solution"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 2,
16+
"id": "setup",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# Example test case\n",
21+
"s = \"ADOBECODEBANC\"\n",
22+
"t = \"ABC\"\n",
23+
"expected = \"BANC\""
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 3,
29+
"id": "execute",
30+
"metadata": {},
31+
"outputs": [
32+
{
33+
"data": {
34+
"text/plain": [
35+
"'BANC'"
36+
]
37+
},
38+
"execution_count": 3,
39+
"metadata": {},
40+
"output_type": "execute_result"
41+
}
42+
],
43+
"source": [
44+
"result = Solution().min_window(s, t)\n",
45+
"result"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 4,
51+
"id": "test",
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"assert result == expected"
56+
]
57+
}
58+
],
59+
"metadata": {
60+
"kernelspec": {
61+
"display_name": "leetcode-py-py3.13",
62+
"language": "python",
63+
"name": "python3"
64+
},
65+
"language_info": {
66+
"codemirror_mode": {
67+
"name": "ipython",
68+
"version": 3
69+
},
70+
"file_extension": ".py",
71+
"mimetype": "text/x-python",
72+
"name": "python",
73+
"nbconvert_exporter": "python",
74+
"pygments_lexer": "ipython3",
75+
"version": "3.13.7"
76+
}
77+
},
78+
"nbformat": 4,
79+
"nbformat_minor": 5
80+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from collections import Counter
2+
3+
4+
class Solution:
5+
# Sliding Window
6+
# Time: O(m + n) where m = len(s), n = len(t)
7+
# Space: O(k) where k is unique chars in t
8+
def min_window(self, s: str, t: str) -> str:
9+
if not t or len(t) > len(s):
10+
return ""
11+
12+
need = Counter(t)
13+
14+
left = 0
15+
formed = 0
16+
required = len(need)
17+
window_counts: dict[str, int] = {}
18+
19+
# Result: (window length, left, right)
20+
ans: tuple[float, int | None, int | None] = (float("inf"), None, None)
21+
22+
for right in range(len(s)):
23+
char = s[right]
24+
window_counts[char] = window_counts.get(char, 0) + 1
25+
26+
# Check if current char frequency matches desired frequency in t
27+
if char in need and window_counts[char] == need[char]:
28+
formed += 1
29+
30+
# Contract window until it's no longer valid
31+
while left <= right and formed == required:
32+
char = s[left]
33+
34+
# Update result if this window is smaller
35+
if right - left + 1 < ans[0]:
36+
ans = (right - left + 1, left, right)
37+
38+
# Remove from left
39+
window_counts[char] -= 1
40+
if char in need and window_counts[char] < need[char]:
41+
formed -= 1
42+
43+
left += 1
44+
45+
if ans[0] == float("inf"):
46+
return ""
47+
assert ans[1] is not None and ans[2] is not None
48+
return s[ans[1] : ans[2] + 1]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import pytest
2+
3+
from leetcode_py.test_utils import logged_test
4+
5+
from .solution import Solution
6+
7+
8+
class TestMinimumWindowSubstring:
9+
def setup_method(self):
10+
self.solution = Solution()
11+
12+
@pytest.mark.parametrize(
13+
"s, t, expected",
14+
[
15+
# Basic cases
16+
("ADOBECODEBANC", "ABC", "BANC"),
17+
("a", "a", "a"),
18+
("a", "aa", ""),
19+
# Edge cases
20+
("", "a", ""), # Empty s
21+
("a", "", ""), # Empty t
22+
("", "", ""), # Both empty
23+
("ab", "ba", "ab"), # Same length
24+
("abc", "cba", "abc"), # Entire string needed
25+
# Duplicates
26+
("ADOBECODEBANC", "AABC", "ADOBECODEBA"), # Correct: needs 2 A's, 1 B, 1 C
27+
("aa", "aa", "aa"),
28+
# No solution
29+
("abc", "def", ""),
30+
("a", "b", ""),
31+
# Multiple valid windows
32+
("ADOBECODEBANC", "AB", "BA"), # Correct: "BA" is shorter than "ADOB"
33+
("abcdef", "cf", "cdef"),
34+
],
35+
)
36+
@logged_test
37+
def test_min_window(self, s: str, t: str, expected: str):
38+
result = self.solution.min_window(s, t)
39+
assert result == expected

0 commit comments

Comments
 (0)