Skip to content

Commit ce9c11d

Browse files
committed
https://leetcode.com/problems/word-search/
1 parent f841205 commit ce9c11d

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
namespace LeetCodeSolutions.Backtracking
2+
{
3+
public class WordSearch
4+
{
5+
// Time: O(m × n × 3^k)
6+
// Space: O(k)
7+
public bool Exist(char[][] board, string word)
8+
{
9+
// Check for empty board or empty word
10+
if (board == null || board.Length == 0 || string.IsNullOrEmpty(word))
11+
return false;
12+
13+
int rows = board.Length;
14+
int cols = board[0].Length;
15+
16+
// Iterate through each cell in the board
17+
for (int i = 0; i < rows; i++)
18+
{
19+
for (int j = 0; j < cols; j++)
20+
{
21+
// Start DFS from cells matching the first character
22+
if (board[i][j] == word[0] && Dfs(board, i, j, word, 0))
23+
return true;
24+
}
25+
}
26+
27+
return false;
28+
}
29+
30+
private bool Dfs(char[][] board, int row, int col, string word, int index)
31+
{
32+
// Base case: entire word found
33+
if (index == word.Length - 1)
34+
return true;
35+
36+
// Temporarily mark current cell as visited
37+
char original = board[row][col];
38+
board[row][col] = '#'; // Using '#' as visited marker
39+
40+
// Define directions: up, right, down, left
41+
int[] dirs = { -1, 0, 1, 0, -1 };
42+
43+
// Explore all four directions
44+
for (int d = 0; d < 4; d++)
45+
{
46+
int newRow = row + dirs[d];
47+
int newCol = col + dirs[d + 1];
48+
49+
// Check boundaries and character match
50+
if (newRow >= 0 && newRow < board.Length &&
51+
newCol >= 0 && newCol < board[0].Length &&
52+
board[newRow][newCol] == word[index + 1])
53+
{
54+
// Recursive DFS call for next character
55+
if (Dfs(board, newRow, newCol, word, index + 1))
56+
return true;
57+
}
58+
}
59+
60+
// Backtrack: restore original character
61+
board[row][col] = original;
62+
return false;
63+
}
64+
65+
[Test(Description = "https://leetcode.com/problems/word-search/")]
66+
[Category("Medium")]
67+
[Category("LeetCode")]
68+
[Category("Word Search")]
69+
[TestCaseSource(nameof(Input))]
70+
[Category("TopInterview")]
71+
[Category("Backtracking")]
72+
public void Test1((bool Output, (char[][], string) Input) item)
73+
{
74+
var response = Exist(item.Input.Item1, item.Input.Item2);
75+
Assert.That(response, Is.EqualTo(item.Output));
76+
}
77+
78+
public static IEnumerable<(bool Output, (char[][], string) Input)> Input
79+
{
80+
get
81+
{
82+
return new List<(bool Output, (char[][], string) Input)>()
83+
{
84+
(true, (new char[][]
85+
{
86+
new char[] { 'A', 'B', 'C', 'E' },
87+
new char[] { 'S', 'F', 'C', 'S' },
88+
new char[] { 'A', 'D', 'E', 'E' },
89+
}, "ABCCED")),
90+
};
91+
}
92+
}
93+
}
94+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ The Top Interview 150 collection on Leetcode is a curated set of 150 interview q
164164
| 104 | Combination Sum | Medium ||
165165
| 105 | N-Queens II | Hard ||
166166
| 106 | Generate Parentheses | Medium | |
167-
| 107 | Word Search | Medium | |
167+
| 107 | Word Search | Medium | |
168168
| <br> Divide &amp; Conquer<br> | | | |
169169
| 108 | Convert Sorted Array to Binary Search Tree | Easy ||
170170
| 109 | Sort List | Medium ||

0 commit comments

Comments
 (0)