Skip to content

Commit 9fd8554

Browse files
authored
feat: add Longest Palindromic Substring (#23)
1 parent 3e18fdb commit 9fd8554

File tree

22 files changed

+819
-349
lines changed

22 files changed

+819
-349
lines changed

.amazonq/rules/problem-creation.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,19 @@ poetry run python .templates/leetcode/scrape.py -s "two-sum"
3232

3333
Required fields for `.templates/leetcode/json/{problem_name}.json`:
3434

35+
**CRITICAL: Use single quotes for Python strings in playground fields to avoid JSON escaping issues with Jupyter notebooks.**
36+
37+
**JSON Escaping Rules:**
38+
39+
- `playground_test_case`: Use single quotes for string literals (e.g., `s = 'hello'` not `s = "hello"`)
40+
- `playground_execution`: Use single quotes for string literals
41+
- `playground_assertion`: Use single quotes for string literals
42+
- Double quotes in JSON + cookiecutter + Jupyter notebook = triple escaping issues
43+
3544
**Reference examples in `.templates/leetcode/examples/` for complete templates:**
3645

37-
- `basic.json5` - Array, string, number problems
46+
- `basic.json5` - All standard problems (array, string, tree, linked list, etc.)
3847
- `design.json5` - Data structure design problems (LRU Cache, etc.)
39-
- `tree.json5` - Binary tree problems
40-
- `linked_list.json5` - Linked list problems
41-
- `matrix.json5` - 2D array/matrix problems
4248

4349
````json
4450
{
Lines changed: 30 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,41 @@
1-
# LeetCode Problem Template Examples
1+
# JSON Template Examples
22

3-
This directory contains comprehensive JSON5 template examples for different types of LeetCode problems. These examples serve as references when creating new problems using the universal cookiecutter template.
3+
This directory contains comprehensive examples for creating LeetCode problem templates.
44

5-
## Template Types
5+
## Files
66

7-
### 1. `basic.json5` - Basic Algorithm Problems
7+
- **`basic.json5`** - Covers all standard problem types:
8+
- Array problems (Container With Most Water)
9+
- String problems (with JSON escaping notes)
10+
- Tree problems (import and parameter examples)
11+
- Linked list problems (import and parameter examples)
12+
- Matrix problems
13+
- Number problems
814

9-
**Use for:** Array, string, number, hash table problems
10-
**Examples:** Container With Most Water, Two Sum, Valid Palindrome
11-
**Key features:**
15+
- **`design.json5`** - Data structure design problems:
16+
- Custom class names (LRUCache, not Solution)
17+
- Multiple methods including `__init__`
18+
- Complex test setup with operation sequences
19+
- Custom imports
1220

13-
- Simple `Solution` class with single method
14-
- Standard test parametrization
15-
- Basic playground setup
21+
## Key Differences
1622

17-
### 2. `tree.json5` - Binary Tree Problems
23+
### Standard Problems (basic.json5)
1824

19-
**Use for:** Binary tree, BST, tree traversal problems
20-
**Examples:** Invert Binary Tree, Maximum Depth, Serialize Tree
21-
**Key features:**
25+
- `solution_class_name`: Always "Solution"
26+
- Single method (usually)
27+
- Simple test cases with direct assertions
28+
- Standard imports
2229

23-
- `TreeNode` imports and conversions
24-
- `TreeNode.from_list()` and `TreeNode.to_list()` in tests
25-
- Tree visualization support
30+
### Design Problems (design.json5)
2631

27-
### 3. `linked_list.json5` - Linked List Problems
32+
- `solution_class_name`: Custom class name (e.g., "LRUCache")
33+
- Multiple methods including constructor
34+
- Operation sequence testing
35+
- Import custom class in tests
2836

29-
**Use for:** Singly/doubly linked list problems
30-
**Examples:** Reverse Linked List, Merge Lists, Detect Cycle
31-
**Key features:**
37+
## Critical Notes
3238

33-
- `ListNode` imports and conversions
34-
- `ListNode.from_list()` and `ListNode.to_list()` in tests
35-
- Arrow visualization support
36-
37-
### 4. `design.json5` - Data Structure Design Problems
38-
39-
**Use for:** Design problems requiring custom classes
40-
**Examples:** LRU Cache, Implement Trie, Design HashMap
41-
**Key features:**
42-
43-
- Custom class names (not `Solution`)
44-
- Multiple methods including `__init__`
45-
- Complex operation sequence testing
46-
- Type annotations for complex test logic
47-
48-
### 5. `matrix.json5` - 2D Array/Matrix Problems
49-
50-
**Use for:** Matrix manipulation, 2D array problems
51-
**Examples:** Spiral Matrix, Rotate Image, Search 2D Matrix
52-
**Key features:**
53-
54-
- 2D array type annotations (`list[list[int]]`)
55-
- Visual examples with images
56-
- Matrix-specific test cases
57-
58-
## Usage Guidelines
59-
60-
### Problem Type Detection
61-
62-
1. **Basic**: Single algorithm, simple input/output
63-
2. **Tree**: Mentions "tree", "node", uses tree terminology
64-
3. **Linked List**: Mentions "linked list", "node", list operations
65-
4. **Design**: "Design", "Implement", multiple operations
66-
5. **Matrix**: "matrix", "2D array", "grid", visual layout
67-
68-
### Key Template Fields
69-
70-
#### Required Fields
71-
72-
- `problem_name`: snake_case identifier
73-
- `solution_class_name`: "Solution" or custom class name
74-
- `problem_number`: LeetCode number as string
75-
- `problem_title`: Exact LeetCode title
76-
- `difficulty`: "Easy", "Medium", or "Hard"
77-
- `topics`: Comma-separated topic string
78-
- `solution_methods`: Array of method definitions
79-
80-
#### Important Patterns
81-
82-
- **Type Hints**: Use modern syntax (`list[int]`, `dict[str, int]`, `Type | None`)
83-
- **Method Names**: Always snake_case
84-
- **Test Cases**: String representation of Python data structures
85-
- **Imports**: Include necessary helper classes (TreeNode, ListNode)
86-
87-
#### PascalCase Naming Rules
88-
89-
For `solution_class_name` and `test_class_name` properties:
90-
91-
- **Acronyms**: Keep all caps ("LRUCache" not "LruCache")
92-
- **Roman numerals**: Keep all caps ("ReverseLinkedListII" not "ReverseLinkedListIi")
93-
- **Common patterns**: "BST", "DFS", "BFS", "API", "URL", "HTML", "JSON", "XML"
94-
95-
### Template Selection Process
96-
97-
1. Identify problem type from description/title
98-
2. Choose appropriate template from examples
99-
3. Customize fields for specific problem
100-
4. Ensure imports match problem requirements
101-
5. Verify test setup matches data structures used
102-
103-
## Validation
104-
105-
All templates are validated against:
106-
107-
- Cookiecutter template compatibility
108-
- Linting requirements (black, isort, ruff, mypy)
109-
- Test framework integration
110-
- Notebook JSON format compliance
111-
112-
## Notes
113-
114-
- JSON5 format allows comments for documentation
115-
- All examples are based on working, tested templates
116-
- Templates are designed for the universal cookiecutter system
117-
- Examples include both simple and complex problem patterns
39+
- **JSON Escaping**: Use single quotes for Python strings in playground fields
40+
- **Type Hints**: Use modern syntax (`list[int]`, `TreeNode | None`)
41+
- **PascalCase**: Keep acronyms ALL CAPS (LRUCache, ReverseLinkedListII)

.templates/leetcode/examples/basic.json5

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,22 @@
3737
"readme_additional": "", // Optional: additional notes, follow-up questions
3838

3939
// === SOLUTION TEMPLATE ===
40-
"solution_imports": "", // Empty for basic problems, add imports if needed
40+
"solution_imports": "", // Empty for basic problems
41+
// For tree: "from leetcode_py import TreeNode"
42+
// For linked list: "from leetcode_py import ListNode"
4143
"solution_methods": [
4244
{
4345
"name": "max_area", // snake_case method name
4446
"parameters": "height: list[int]", // Modern Python type hints (list[int], not List[int])
47+
// For tree: "root: TreeNode | None"
48+
// For linked list: "head: ListNode | None"
49+
// For string: "s: str"
4550
"return_type": "int", // Return type annotation
46-
"dummy_return": "0" // Default return value (auto-set by generator)
51+
"dummy_return": "0" // Default return value
52+
// For string: "\"\""
53+
// For bool: "False"
54+
// For list: "[]"
55+
// For tree/linked list: "None"
4756
}
4857
],
4958

@@ -68,8 +77,12 @@
6877
],
6978

7079
// === PLAYGROUND NOTEBOOK ===
80+
// CRITICAL: Use single quotes for Python strings to avoid JSON escaping issues with Jupyter notebooks
81+
// Double quotes in JSON + cookiecutter + Jupyter notebook = triple escaping issues
7182
"playground_imports": "from solution import Solution",
7283
"playground_test_case": "# Example test case\nheight = [1,8,6,2,5,4,8,3,7]\nexpected = 49",
84+
// For string problems: "s = 'hello'\nexpected = 'olleh'"
85+
// For tree: "root_list = [3,9,20,None,None,15,7]\nroot = TreeNode.from_list(root_list)"
7386
"playground_execution": "result = Solution().max_area(height)\nresult",
7487
"playground_assertion": "assert result == expected"
7588
}

.templates/leetcode/examples/design.json5

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@
7575
],
7676

7777
// === PLAYGROUND NOTEBOOK ===
78-
// IMPORTANT: Design playground uses operation sequences like tests
78+
// CRITICAL: Use single quotes for Python strings to avoid JSON escaping issues with Jupyter notebooks
79+
// Double quotes in JSON + cookiecutter + Jupyter notebook = triple escaping issues
7980
playground_imports: "from solution import LRUCache",
8081
playground_test_case: "# Example test case\noperations = ['LRUCache', 'put', 'put', 'get', 'put', 'get', 'put', 'get', 'get', 'get']\ninputs = [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]\nexpected = [None, None, None, 1, None, -1, None, -1, 3, 4]",
8182
playground_execution: "cache = None\nresults: list[int | None] = []\nfor i, op in enumerate(operations):\n if op == 'LRUCache':\n cache = LRUCache(inputs[i][0])\n results.append(None)\n elif op == 'get' and cache is not None:\n results.append(cache.get(inputs[i][0]))\n elif op == 'put' and cache is not None:\n cache.put(inputs[i][0], inputs[i][1])\n results.append(None)\nresults",

.templates/leetcode/examples/linked_list.json5

Lines changed: 0 additions & 78 deletions
This file was deleted.

.templates/leetcode/examples/matrix.json5

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)