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 is those horizontally or V Ertically neighboring. The same letter cell is used more than once.
For example,
Given board =
[ [' A ', ' B ', ' C ', ' e '], [' s ', ' F ', ' C ', ' s '], [' A ', ' D ', ' e ', ' e ']]
Word = "ABCCED"
, returns true
,
Word = "SEE"
, returns true
,
Word = "ABCB"
, returns false
.
Solution 1: Loop. Go through the whole board first, find the matching position with word[0], and then, based on this, find out the position of the current position and the next letter of Word, and put it in the stack as the location to choose. Then loop the stack and select one of the positions as the match, and use this as a benchmark for the depth-first search until word matches all the letters successfully.
classSolution { Public: BOOLexist (vector< vector<Char> >& Board,stringword) { if(Board.empty () | | board[0].empty () | | Word.empty ())return false; intm = Board.size (), n = board[0].size (), k =word.size (); Stack< pair<int,int> > Path;//store all possible pathsstack< pair<int,int> > Dirc;//stores all the next candidate locations in the current location//Loop through the board, match the first letter for(inti =0; I < m; ++i) { for(intj =0; J < N; ++j) {if(Board[i][j]! = word[0])Continue; intStarti = i, startj = j, L =1; Vector<int> Direction (k,0); Vector< vector<int> > isvisited (M, vector<int> (n,0)); ISVISITED[STARTI][STARTJ]=1; //match all the letters that followLoop:if(L = = k)return true; if(Startj >0&& BOARD[STARTI][STARTJ-1] = = Word[l] && ISVISITED[STARTI][STARTJ-1] ==0) {// LeftDirc.push (Make_pair (Starti, STARTJ-1)); Path.push (Make_pair (Starti, STARTJ-1)); ++Direction[l]; } if(STARTJ < n-1&& BOARD[STARTI][STARTJ +1] = = Word[l] && ISVISITED[STARTI][STARTJ +1] ==0) {// RightDirc.push (Make_pair (Starti, STARTJ +1)); Path.push (Make_pair (Starti, STARTJ+1)); ++Direction[l]; } if(Starti >0&& Board[starti-1][STARTJ] = = Word[l] && Isvisited[starti-1][STARTJ] = =0) {//TopDirc.push (Make_pair (Starti-1, STARTJ)); Path.push (Make_pair (Starti-1, STARTJ)); ++Direction[l]; } if(Starti < M-1&& Board[starti +1][STARTJ] = = Word[l] && Isvisited[starti +1][STARTJ] = =0) {//BottomDirc.push (Make_pair (Starti +1, STARTJ)); Path.push (Make_pair (Starti+1, STARTJ)); ++Direction[l]; } while(L >=0&& Direction[l] = =0) {//There is a mismatch in the current path, and the fallback to the nearest fork positionL-=1; if(!path.empty ()) {//Those locations that are rolled back need to clear the Access identity intIdxi = Path.top (). First, IDXJ =Path.top (). Second; ISVISITED[IDXI][IDXJ]=0; Path.pop (); } } while(L >=0&& direction[l]-->0) {//there is at least one matching position between the top and bottom of the current position, then the advance continues to match++l; Starti=Dirc.top (). First; STARTJ=Dirc.top (). Second; ISVISITED[STARTI][STARTJ]=1;//identifies the current location has been accessedDirc.pop (); GotoLoop; } } } return false; }};
Solution 2: Use recursion to implement DFS. You can reduce the space complexity to O (1) by modifying the board current position character to a specific tag.
classSolution { Public: BOOLexist (vector< vector<Char> >& Board,stringword) { if(Board.empty () | | board[0].empty () | | Word.empty ())return false; intm = Board.size (), n = board[0].size (), k =word.size (); Vector< vector<int> > isvisited (M, vector<int> (n,0)); for(inti =0; I < m; ++i) { for(intj =0; J < N; ++j) {if(Search (board, Word, isvisited,0, I, J))return true; } } return false; }Private: BOOLSearch (vector< vector<Char> >& Board,Const string& Word, vector< vector<int> >& isvisited,intKintIintj) {intm = Board.size (), n = board[0].size (); if(k = = Word.size ())return true; if(I <0|| I >= m | | J <0|| J >= N | | ISVISITED[I][J] = =1|| BOARD[I][J]! = Word[k])return false; ISVISITED[I][J]=1; BOOLres =Search (board, Word, isvisited, K+1, I, J-1) ||Search (board, Word, isvisited, K+1, I, J +1) ||Search (board, Word, isvisited, K+1I1, j) | |Search (board, Word, isvisited, K+1, i +1, J); ISVISITED[I][J]=0; returnRes; }};
[Leetcode]105. Word Search Word Lookup