Skip to content

Commit 212f56f

Browse files
committed
feat: add 5 more leetcode problems
1 parent 09d9496 commit 212f56f

38 files changed

+1163
-14
lines changed

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 ?= reverse_nodes_in_k_group
2+
PROBLEM ?= number_of_connected_components_in_an_undirected_graph
33
FORCE ?= 0
44
COMMA := ,
55

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ lcpy gen --problem-tag neetcode-150 --output leetcode # Generate NeetCode 150
8282
lcpy gen --problem-tag blind-75 --output leetcode # Generate Blind 75 problems
8383
```
8484

85-
![Problem Generation](https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/problems-generation.png)
85+
<img src="https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/problems-generation.png" alt="Problem Generation" style="pointer-events: none;">
8686

8787
_Bulk generation output showing "Generated problem:" messages for all 75 Grind problems_
8888

89-
![Problem Generation 2](https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/problems-generation-2.png)
89+
<img src="https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/problems-generation-2.png" alt="Problem Generation 2" style="pointer-events: none;">
9090

9191
_Generated folder structure showing all 75 problem directories after command execution_
9292

@@ -104,19 +104,19 @@ leetcode/two_sum/
104104
└── __init__.py # Package marker
105105
```
106106

107-
![README Example](https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/readme-example.png)
107+
<img src="https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/readme-example.png" alt="README Example" style="pointer-events: none;">
108108

109109
_README format that mirrors LeetCode's problem description layout_
110110

111-
![Solution Boilerplate](https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/solution-boilerplate.png)
111+
<img src="https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/solution-boilerplate.png" alt="Solution Boilerplate" style="pointer-events: none;">
112112

113113
_Solution boilerplate with type hints and TODO placeholder_
114114

115-
![Test Example](https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/test-example.png)
115+
<img src="https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/test-example.png" alt="Test Example" style="pointer-events: none;">
116116

117117
_Comprehensive parametrized tests with 10+ test cases - executable and debuggable in local development environment_
118118

119-
![Test Logging](https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/logs-in-test-solution.png)
119+
<img src="https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/logs-in-test-solution.png" alt="Test Logging" style="pointer-events: none;">
120120

121121
_Beautiful colorful test output with loguru integration for enhanced debugging and test result visualization_
122122

@@ -141,21 +141,21 @@ Professional-grade visualization for debugging complex data structures with dual
141141

142142
#### Jupyter Notebook Integration (HTML Rendering)
143143

144-
![Tree Visualization](https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/tree-viz.png)
144+
<img src="https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/tree-viz.png" alt="Tree Visualization" style="pointer-events: none;">
145145

146146
_Interactive tree visualization using Graphviz SVG rendering in Jupyter notebooks_
147147

148-
![LinkedList Visualization](https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/linkedlist-viz.png)
148+
<img src="https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/linkedlist-viz.png" alt="LinkedList Visualization" style="pointer-events: none;">
149149

150150
_Professional linked list visualization with Graphviz in Jupyter environment_
151151

152152
#### Terminal/Console Output (String Rendering)
153153

154-
![Tree String Visualization](https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/tree-str-viz.png)
154+
<img src="https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/tree-str-viz.png" alt="Tree String Visualization" style="pointer-events: none;">
155155

156156
_Clean ASCII tree rendering using anytree for terminal debugging and logging_
157157

158-
![LinkedList String Visualization](https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/linkedlist-str-viz.png)
158+
<img src="https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/linkedlist-str-viz.png" alt="LinkedList String Visualization" style="pointer-events: none;">
159159

160160
_Simple arrow-based list representation for console output and test debugging_
161161

@@ -166,7 +166,7 @@ _Simple arrow-based list representation for console output and test debugging_
166166
- **Repository State**: This repo converts them to Python files (`.py`) for better version control
167167
- **Dual Rendering**: Automatic HTML visualization in notebooks, clean string output in terminals
168168

169-
![Notebook Example](https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/notebook-example.png)
169+
<img src="https://raw.githubusercontent.com/wislertt/leetcode-py/main/docs/images/notebook-example.png" alt="Notebook Example" style="pointer-events: none;">
170170

171171
_Interactive multi-cell playground with rich data structure visualization for each problem_
172172

@@ -301,8 +301,8 @@ make gen-all-problems # Regenerate all problems (destructive)
301301

302302
## 🏗️ Architecture
303303

304-
- **Template-Driven**: JSON templates in `.templates/leetcode/json/` drive code generation
305-
- **Cookiecutter Integration**: Uses `.templates/leetcode/{{cookiecutter.problem_name}}/` template for consistent file structure
304+
- **Template-Driven**: JSON templates in `leetcode_py/cli/resources/leetcode/json/problems/` drive code generation
305+
- **Cookiecutter Integration**: Uses `leetcode_py/cli/resources/leetcode/{{cookiecutter.problem_name}}/` template for consistent file structure
306306
- **Automated Scraping**: LLM-assisted problem data extraction from LeetCode
307307
- **Version Control Friendly**: Python files by default, optional notebook support
308308

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Encode and Decode Strings
2+
3+
**Difficulty:** Medium
4+
**Topics:** Array, String, Design
5+
**Tags:** blind-75
6+
7+
**LeetCode:** [Problem 271](https://leetcode.com/problems/encode-and-decode-strings/description/)
8+
9+
## Problem Description
10+
11+
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings.
12+
13+
## Examples
14+
15+
### Example 1:
16+
17+
```
18+
Input: dummy_input = ["Hello","World"]
19+
Output: "Hello,World"
20+
Explanation: Machine 1:
21+
Codec encoder = new Codec();
22+
String msg = encoder.encode(strs);
23+
Machine 1 ---msg---> Machine 2
24+
Machine 2:
25+
Codec decoder = new Codec();
26+
String[] strs = decoder.decode(msg);
27+
```
28+
29+
## Constraints
30+
31+
- 1 <= strs.length <= 200
32+
- 0 <= strs[i].length <= 200
33+
- strs[i] contains any possible characters out of 256 valid ASCII characters.
34+
35+
**Note:**
36+
37+
- The string may contain any possible characters out of 256 valid ASCII characters. Your algorithm should be generalized enough to work on any possible characters.
38+
- Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
39+
- Do not rely on any library method such as eval or serialize methods. You should implement your own encode/decode algorithm.

leetcode/encode_and_decode_strings/__init__.py

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def run_encode_decode(solution_class: type, strs: list[str]):
2+
implementation = solution_class()
3+
encoded = implementation.encode(strs)
4+
decoded = implementation.decode(encoded)
5+
return decoded
6+
7+
8+
def assert_encode_decode(result: list[str], expected: list[str]) -> bool:
9+
assert result == expected
10+
return True
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# ---
2+
# jupyter:
3+
# jupytext:
4+
# text_representation:
5+
# extension: .py
6+
# format_name: percent
7+
# format_version: '1.3'
8+
# jupytext_version: 1.17.3
9+
# kernelspec:
10+
# display_name: leetcode-py-py3.13
11+
# language: python
12+
# name: python3
13+
# ---
14+
15+
# %%
16+
from helpers import assert_encode_decode, run_encode_decode
17+
from solution import Solution
18+
19+
# %%
20+
# Example test case
21+
strs = ["Hello", "World"]
22+
expected = strs
23+
24+
# %%
25+
result = run_encode_decode(Solution, strs)
26+
result
27+
28+
# %%
29+
assert_encode_decode(result, expected)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
def encode(self, strs: list[str]) -> str:
6+
encoded = ""
7+
for s in strs:
8+
# Format: length + '#' + string
9+
encoded += str(len(s)) + "#" + s
10+
return encoded
11+
12+
# Time: O(n)
13+
# Space: O(n)
14+
def decode(self, s: str) -> list[str]:
15+
decoded = []
16+
i = 0
17+
18+
while i < len(s):
19+
# Find the delimiter '#'
20+
j = i
21+
while s[j] != "#":
22+
j += 1
23+
24+
# Extract length
25+
length = int(s[i:j])
26+
27+
# Extract string of that length
28+
decoded.append(s[j + 1 : j + 1 + length])
29+
30+
# Move to next encoded string
31+
i = j + 1 + length
32+
33+
return decoded
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pytest
2+
3+
from leetcode_py import logged_test
4+
5+
from .helpers import assert_encode_decode, run_encode_decode
6+
from .solution import Solution
7+
8+
9+
class TestEncodeAndDecodeStrings:
10+
def setup_method(self):
11+
self.solution = Solution()
12+
13+
@logged_test
14+
@pytest.mark.parametrize(
15+
"strs",
16+
[
17+
["Hello", "World"],
18+
["abc", "def"],
19+
[""],
20+
["a", "b", "c"],
21+
["", "", ""],
22+
["hello", "", "world"],
23+
["a#b", "c#d"],
24+
["4#3hello", "world"],
25+
["#", "##", "###"],
26+
["abc123", "def456", "ghi789"],
27+
["special!@#$%^&*()"],
28+
["with spaces", "and tabs\t", "and newlines\n"],
29+
["unicode测试", "emoji😀"],
30+
["very long string " * 10],
31+
["a" * 200],
32+
[],
33+
["single"],
34+
["comma,separated", "values,here"],
35+
["pipe|separated", "values|here"],
36+
["colon:separated", "values:here"],
37+
],
38+
)
39+
def test_encode_decode(self, strs: list[str]):
40+
result = run_encode_decode(Solution, strs)
41+
assert_encode_decode(result, strs)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Graph Valid Tree
2+
3+
**Difficulty:** Medium
4+
**Topics:** Depth-First Search, Breadth-First Search, Union Find, Graph
5+
**Tags:** blind-75
6+
7+
**LeetCode:** [Problem 261](https://leetcode.com/problems/graph-valid-tree/description/)
8+
9+
## Problem Description
10+
11+
Given `n` nodes labeled from `0` to `n-1` and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.
12+
13+
## Examples
14+
15+
### Example 1:
16+
17+
```
18+
Input: n = 5, and edges = [[0,1], [0,2], [0,3], [1,4]]
19+
Output: true
20+
```
21+
22+
### Example 2:
23+
24+
```
25+
Input: n = 5, and edges = [[0,1], [1,2], [2,3], [1,3], [1,4]]
26+
Output: false
27+
```
28+
29+
## Constraints
30+
31+
- 0 <= n <= 2000
32+
- 0 <= edges.length <= 5000
33+
- edges[i].length == 2
34+
- 0 <= ai, bi < n
35+
- ai != bi
36+
- There are no self-loops or repeated edges.
37+
38+
**Note:** you can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0,1] is the same as [1,0] and thus will not appear together in edges.

leetcode/graph_valid_tree/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)