Created
June 13, 2017 22:26
-
-
Save jianminchen/9cae4f686ebabc2275cfc6e45dd0992e to your computer and use it in GitHub Desktop.
Leetcode 213 - word search II - practice, first failed duplicate words in the dictionary, add Distinct() on line 35. And then time out error.
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Leetcode213_AllWordsInBoard | |
{ | |
/// <summary> | |
/// https://leetcode.com/problems/word-search-ii/#/description | |
/// failed test case | |
/// ["a","a"] | |
/// output: ["a","a"] | |
/// expected: ["a"] | |
/// </summary> | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string[] words = new string[]{"oath","pea","eat","rain"}; | |
var board = new char[,]{ | |
{'o','a','a','n'}, | |
{'e','t','a','e'}, | |
{'i','h','k','r'}, | |
{'i','f','l','v'}}; | |
var found = FindWords(board, words); | |
} | |
public static IList<string> FindWords(char[,] board, string[] words) | |
{ | |
var wordsFound = new List<string>(); | |
foreach (var word in words.Distinct()) // bug: remove duplicate words | |
{ | |
if (findWord(board, word)) | |
{ | |
wordsFound.Add(word); | |
} | |
} | |
return wordsFound; | |
} | |
private static bool findWord(char[,] board, string word) | |
{ | |
if (board == null || | |
board.GetLength(0) == 0 || | |
board.GetLength(1) == 0 || | |
word == null || | |
word.Length == 0) | |
{ | |
return false; | |
} | |
int rows = board.GetLength(0); | |
int cols = board.GetLength(1); | |
for (int row = 0; row < rows; row++) | |
{ | |
for (int col = 0; col < cols; col++) | |
{ | |
var visit = board[row, col]; | |
bool startSearch = visit == word[0]; | |
if (!startSearch) | |
{ | |
continue; | |
} | |
var memo = new bool[rows, cols]; | |
bool foundOne = searchWord(board, word, row, col, 0, memo); | |
if (foundOne) | |
{ | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="board"></param> | |
/// <param name="word"></param> | |
/// <param name="startRow"></param> | |
/// <param name="startCol"></param> | |
/// <returns></returns> | |
private static bool searchWord(char[,] board, string word, int startRow, int startCol, int index, bool[,] memo) | |
{ | |
int rows = board.GetLength(0); | |
int cols = board.GetLength(1); | |
if(! isInBoundary(board, startRow, startCol) || memo[startRow,startCol]) | |
{ | |
return false; | |
} | |
var visit = board[startRow, startCol]; | |
var length = word.Length; | |
bool isEqual = index < length && visit == word[index]; | |
bool isLastChar = index == (length - 1); | |
if (!isEqual) | |
{ | |
return false; | |
} | |
memo[startRow, startCol] = true; | |
// base case | |
if (isLastChar) | |
{ | |
return true; | |
} | |
var nextIndex = index + 1; // bug found through debug - nextIndex++ in four recursive calls | |
var result = | |
searchWord(board, word, startRow, startCol - 1, nextIndex, memo) || | |
searchWord(board, word, startRow, startCol + 1, nextIndex, memo) || | |
searchWord(board, word, startRow - 1, startCol, nextIndex, memo) || | |
searchWord(board, word, startRow + 1, startCol, nextIndex, memo); | |
if (!result) | |
{ | |
memo[startRow, startCol] = false; | |
} | |
return result; | |
} | |
private static bool isInBoundary(char[,] board, int startRow, int startCol) | |
{ | |
int rows = board.GetLength(0); | |
int cols = board.GetLength(1); | |
return startRow >= 0 && startRow < rows && | |
startCol >= 0 && startCol < cols; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment