-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solve LeetCode #79 'Word Search' [Medium].
- Loading branch information
1 parent
4dd8352
commit 61ba239
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
LeetCode/src/LeetCode.Challenges/Problems00xx/N_0079_WordSearch/Solution.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
namespace LeetCode.Challenges.Problems00xx.N_0079_WordSearch; | ||
|
||
public static class Solution | ||
{ | ||
public static bool Exist(char[][] board, string word) | ||
{ | ||
var rows = board.Length; | ||
var cols = board[0].Length; | ||
var visited = new bool[rows, cols]; | ||
|
||
for (int row = 0; row < rows; row++) | ||
{ | ||
for (int col = 0; col < cols; col++) | ||
{ | ||
if (Dfs(row, col, 0)) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
|
||
bool Dfs(int i, int j, int index) | ||
{ | ||
if (i < 0 || i >= rows || j < 0 || j >= cols || visited[i, j] || board[i][j] != word[index]) | ||
{ | ||
return false; | ||
} | ||
|
||
if (index == word.Length - 1) | ||
{ | ||
return true; | ||
} | ||
|
||
visited[i, j] = true; | ||
|
||
bool found = | ||
Dfs(i + 1, j, index + 1) || | ||
Dfs(i - 1, j, index + 1) || | ||
Dfs(i, j + 1, index + 1) || | ||
Dfs(i, j - 1, index + 1); | ||
|
||
visited[i, j] = false; | ||
|
||
return found; | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
LeetCode/tests/LeetCode.Challenges.UnitTests/Problems00xx/N_0079_WordSearch/SolutionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using LeetCode.Challenges.Problems00xx.N_0079_WordSearch; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace LeetCode.Challenges.UnitTests.Problems00xx.N_0079_WordSearch; | ||
|
||
public class SolutionTests | ||
{ | ||
[Theory] | ||
[MemberData(nameof(TestData))] | ||
public void GivenNumbers_WhenSubsets_ThenResultAsExpected(char[][] board, string word, bool expectedResult) | ||
{ | ||
Solution.Exist(board, word).ShouldBe(expectedResult); | ||
} | ||
|
||
public static IEnumerable<object[]> TestData() | ||
{ | ||
yield return | ||
[ | ||
new char[][] | ||
{ | ||
['A', 'B', 'C', 'E'], | ||
['S', 'F', 'C', 'S'], | ||
['A', 'D', 'E', 'E'] | ||
}, | ||
"ABCCED", | ||
true | ||
]; | ||
|
||
yield return | ||
[ | ||
new char[][] | ||
{ | ||
['A', 'B', 'C', 'E'], | ||
['S', 'F', 'C', 'S'], | ||
['A', 'D', 'E', 'E'] | ||
}, | ||
"SEE", | ||
true | ||
]; | ||
|
||
yield return | ||
[ | ||
new char[][] | ||
{ | ||
[ 'A', 'B', 'C', 'E' ], | ||
[ 'S', 'F', 'C', 'S' ], | ||
[ 'A', 'D', 'E', 'E' ] | ||
}, | ||
"ABCB", | ||
false | ||
]; | ||
} | ||
} |