Repository navigation
Add unit tests for the "Word Search" problem. - #22
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds comprehensive unit tests for the "Word Search" problem (LeetCode 79) and refactors the implementation to use proper package structure and improved performance optimizations.
- Adds JUnit 5 test cases covering the main examples from the problem
- Refactors the solution to use a boolean array instead of a HashSet for visited tracking
- Moves the implementation from root directory to proper Maven package structure
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/test/java/com/leetcode/medium/WordSearch79Test.java | New test file with 3 test cases covering basic functionality and edge cases |
| src/main/java/com/leetcode/medium/WordSearch79.java | Refactored implementation with improved performance using boolean array for visited tracking |
| src/WordSearch79.java | Removed original implementation from root directory |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
|
||
| @Test | ||
| void testExist_Example1() { | ||
| char[][] board = {{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}}; |
There was a problem hiding this comment.
The same board array is duplicated across all three test methods. Consider extracting this as a class-level constant or creating it in the setUp method to reduce code duplication.
|
|
||
| @Test | ||
| void testExist_Example2() { | ||
| char[][] board = {{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}}; |
There was a problem hiding this comment.
The same board array is duplicated across all three test methods. Consider extracting this as a class-level constant or creating it in the setUp method to reduce code duplication.
|
|
||
| @Test | ||
| void testExist_Example3() { | ||
| char[][] board = {{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}}; |
There was a problem hiding this comment.
The same board array is duplicated across all three test methods. Consider extracting this as a class-level constant or creating it in the setUp method to reduce code duplication.
| if (this.dfs(r + 1, c, board, word, index + 1, visited) || this.dfs(r - 1, c, board, word, index + 1, visited) || this.dfs(r, c + 1, board, word, index + 1, visited) || this.dfs(r, c - 1, board, word, index + 1, visited)) { | ||
| return true; |
There was a problem hiding this comment.
This line is too long and hard to read. Consider breaking it into multiple lines or using an array/loop to iterate through the four directions for better readability.
| if (this.dfs(r + 1, c, board, word, index + 1, visited) || this.dfs(r - 1, c, board, word, index + 1, visited) || this.dfs(r, c + 1, board, word, index + 1, visited) || this.dfs(r, c - 1, board, word, index + 1, visited)) { | |
| return true; | |
| int[] dr = {1, -1, 0, 0}; | |
| int[] dc = {0, 0, 1, -1}; | |
| for (int i = 0; i < 4; i++) { | |
| if (this.dfs(r + dr[i], c + dc[i], board, word, index + 1, visited)) { | |
| return true; | |
| } |
No description provided.