LeetCode Word Search (Java)
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
Given board =
[ ["ABCE"], ["SFCS"], ["ADEE"] ]word =
"ABCCED"
, -> returns true
,word =
"SEE"
, -> returns true
,word =
"ABCB"
, -> returns false
.
Solution:
Applied Helper table and DFS to solve it, for each char in board, check if it is matched in word, if so, recursively check the rest chars in word.
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
public class WordSearch { | |
public boolean exist(char[][] board, String word) { | |
if (board==null||board.length==0||word==null||word.length()==0){ | |
return false; | |
} | |
boolean [][] checker=new boolean[board.length][board[0].length]; | |
for (int row=0; row<board.length; row++){ | |
for (int col=0; col<board[0].length; col++){ | |
if (isFind(checker, board, word, 0, row, col)){ | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
private boolean isFind(boolean[][] checker, char[][] board, String word, int i, int row, int col){ | |
if (board[row][col]!=word.charAt(i)|| checker[row][col]){ | |
return false; | |
} | |
checker[row][col]=true; | |
if (i==word.length()-1){ | |
return true; | |
} | |
if (row-1>=0 && isFind(checker, board, word, i+1, row-1, col)){ | |
return true; | |
} | |
if (row+1<board.length && isFind(checker, board, word, i+1,row+1, col )){ | |
return true; | |
} | |
if (col-1>=0 && isFind(checker, board, word, i+1, row, col-1)){ | |
return true; | |
} | |
if (col+1<board[0].length && isFind(checker, board, word, i+1, row, col+1)){ | |
return true; | |
} | |
checker[row][col]=false; | |
return false; | |
} | |
} |
No comments:
Post a Comment