Given a 2D board and a list of words from the dictionary, find all words in the Board.
Each word must is constructed from letters of sequentially adjacent cell, where "adjacent" cells is those horizontally or Vertically Neighboring. The same letter cell is used more than once in a Word.
For example,
Given words = and ["oath","pea","eat","rain"] board =
[ [' o ', ' A ', ' a ', ' n '], [' e ', ' t ', ' a ', ' e '], [' i ', ' H ', ' k ', ' r '], [' i ', ' F ', ' l ', ' v ']]
Return ["eat","oath"] .
Note:
You may assume this all inputs is consist of lowercase letters a-z .
1, the Use and table violence algorithm, sure enough Time-out.
public classSolution { publicList<string> Findwords (Char[] board, string[] Words) {List<String> list =NewArrayList (); for(String Str:words) {if(isexit (str, BOARD)) {if(!list.contains (str)) List.add (str); } } returnlist; } public BooleanIsexit (String word,Char[] Board) { Char[] ch =Word.tochararray (); CharFirst = Ch[0]; for(inti = 0; I < board.length; i++){ for(intj = 0; J < board[0].length; J + +){ if(board[i][j] = =first ) {board[i][j]= ' A '; if(getexit (board, ch, i, j, 1) ) {board[i][j]=first ; return true; } board[i][j]=first ; } } } return false; } public BooleanGetexit (Char[] board,Char[] ch,intIintJintPOS) { if(pos = =Ch.length) { return true; } if(i-1 >= 0 && board[i-1][j] = =Ch[pos]) {board[i-1][j] = ' A '; if(getexit (board, ch, i-1, j, pos + 1) ) {board[i-1][j] =ch[pos]; return true; } board[i-1][j] =ch[pos]; } if(i + 1 < board.length && Board[i + 1][j] = =Ch[pos]) {board[i+ 1][j] = ' A '; if(getexit (board, ch, i + 1, j, pos + 1) ) {board[i+ 1][j] =ch[pos]; return true; } board[i+ 1][j] =ch[pos]; } if(j-1 >= 0 && board[i][j-1] = =Ch[pos]) {board[i][j-1] = ' A '; if(getexit (board, ch, i, j-1, pos + 1) ) {board[i][j-1] =ch[pos]; return true; } board[i][j-1] =ch[pos]; } if(j + 1 < board[0].length && Board[i][j + 1] = =Ch[pos]) {board[i][j+ 1] = ' A '; if(getexit (board, ch, i, j + 1, POS + 1) ) {board[i][j+ 1] =ch[pos]; return true; } board[i][j+ 1] =ch[pos]; } return false; } }
2, the use of the prefix tree, the word build, and then traverse the beginning of all nodes of the string can Be.
public classSolution { publicList<string> Findwords (Char[] board, string[] Words) {List<String> res =NewArraylist<>(); Trienode Root=Buildtrie (words); for(inti = 0; I < board.length; i++) { for(intj = 0; J < board[0].length; J + +) {dfs (board, i, j, root, res); } } returnres;} public voidDfsChar[] board,intIintj, Trienode p, list<string>res) { Charc =board[i][j]; if(c = = ' # ' | | p.next[c-' a '] = =NULL)return; P= p.next[c-' a ']; if(p.word! =NULL) {//found oneRes.add (p.word); P.word=NULL;//de-duplicate} board[i][j]= ' # '; if(i > 0) dfs (board, i-1, j, p, res); if(j > 0) dfs (board, i, j-1, p, res); if(i < Board.length-1) dfs (board, i + 1, j, p, res); if(j < Board[0].length-1) Dfs (board, i, j + 1, p, res); board[i][j]=c;} publictrienode Buildtrie (string[] words) {trienode root=NewTrienode (); for(String W:words) {trienode p=root; for(CharC:w.tochararray ()) { inti = c-' a '; if(p.next[i] = =NULL) p.next[i] =NewTrienode (); P=p.next[i]; } P.word=w; } returnroot;}classTrienode {trienode[] Next=NewTrienode[26]; String word;} }
Leetcode 212. Word Search II Add to List find Word----------java