Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 0 additions & 50 deletions src/WordSearch79.java

This file was deleted.

37 changes: 37 additions & 0 deletions src/main/java/com/leetcode/medium/WordSearch79.java
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;
Comment on lines +13 to +14

Copilot AI Sep 8, 2025

Copy link

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.

Suggested change
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;
}

Copilot uses AI. Check for mistakes.
}
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;
}
}
51 changes: 51 additions & 0 deletions src/test/java/com/leetcode/medium/WordSearch79Test.java
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'}};

Copilot AI Sep 8, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same board array is duplicated across all three test methods. Consider extracting this to a private static final field or creating it in the setUp method to reduce code duplication.

Copilot uses AI. Check for mistakes.

Copilot AI Sep 8, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
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'}};

Copilot AI Sep 8, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same board array is duplicated across all three test methods. Consider extracting this to a private static final field or creating it in the setUp method to reduce code duplication.

Copilot uses AI. Check for mistakes.

Copilot AI Sep 8, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
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'}};

Copilot AI Sep 8, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same board array is duplicated across all three test methods. Consider extracting this to a private static final field or creating it in the setUp method to reduce code duplication.

Copilot uses AI. Check for mistakes.

Copilot AI Sep 8, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
String word = "ABCB";
boolean expected = false;

boolean result = assertTimeoutPreemptively(Duration.ofMillis(100), () -> solution.exist(board, word));

assertEquals(expected, result);
}
}