Repository navigation
Add unit tests for the "Word Search" problem. #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Tags: Backtracking, DFS | ||
| package com.leetcode.medium; | ||
|
|
||
| public class WordSearch79 { | ||
| private boolean dfs(int r, int c, char[][] board, String word, int index, boolean[][] visited) { | ||
| if (index == word.length()) { | ||
| return true; | ||
| } | ||
| if (r < 0 || r == board.length || c < 0 || c == board[0].length || visited[r][c] || word.charAt(index) != board[r][c]) { | ||
| return false; | ||
| } | ||
| visited[r][c] = true; | ||
| 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; | ||
| } | ||
| visited[r][c] = false; | ||
| return false; | ||
| } | ||
| /* | ||
| w: length of word | ||
| Time complexity: O(m*n*4^w) | ||
| Space complexity: O(w) | ||
| */ | ||
| public boolean exist(char[][] board, String word) { | ||
| int rows = board.length; | ||
| int cols = board[0].length; | ||
| boolean[][] visited = new boolean[rows][cols]; | ||
| for (int r = 0; r < board.length; r++) { | ||
| for (int c = 0; c < board[0].length; c++) { | ||
| if (this.dfs(r, c, board, word, 0, visited)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package com.leetcode.medium; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; | ||
|
|
||
| public class WordSearch79Test { | ||
| private WordSearch79 solution; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| solution = new WordSearch79(); | ||
| } | ||
|
|
||
| @Test | ||
| void testExist_Example1() { | ||
| char[][] board = {{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}}; | ||
|
||
| String word = "ABCCED"; | ||
| boolean expected = true; | ||
|
|
||
| boolean result = assertTimeoutPreemptively(Duration.ofMillis(100), () -> solution.exist(board, word)); | ||
|
|
||
| assertEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| void testExist_Example2() { | ||
| char[][] board = {{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}}; | ||
|
||
| String word = "SEE"; | ||
| boolean expected = true; | ||
|
|
||
| boolean result = assertTimeoutPreemptively(Duration.ofMillis(100), () -> solution.exist(board, word)); | ||
|
|
||
| assertEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| void testExist_Example3() { | ||
| char[][] board = {{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}}; | ||
|
||
| String word = "ABCB"; | ||
| boolean expected = false; | ||
|
|
||
| boolean result = assertTimeoutPreemptively(Duration.ofMillis(100), () -> solution.exist(board, word)); | ||
|
|
||
| assertEquals(expected, result); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.