[Leetcode] Word search

Source: Internet
Author: User

Word search

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,
GivenBoard=

[  ["ABCE"],  ["SFCS"],  ["ADEE"]]

Word="ABCCED",-> Returnstrue,
Word="SEE",-> Returnstrue,
Word="ABCB",-> Returnsfalse.

 

After such a long time, the second brush was exactly the same as the first code, and even the details were the same, so incredible.

Algorithm ideas:

Recursion is performed on the top, bottom, left, and right directions. DFS determines the boundary.

 1 public class Solution { 2      public boolean exist(char[][] board, String word) { 3          if(board == null || board.length == 0|| word == null ) return false; 4          int height = board.length; 5          int width = board[0].length; 6          if(height * width < word.length()) return false; 7          for(int i = 0; i < height; i++){ 8              for(int j = 0; j < width; j++){ 9                  if(board[i][j] == word.charAt(0)){10                      if(dfs(board,word.substring(1),i,j)) return true;11                  }12              }13          }14          return false;15      }16      private boolean dfs(char[][] board,String left,int row,int column){17          if(left.length() == 0){18              return true;19          }20          int height = board.length;21          int width = board[0].length;22          char c = board[row][column];23          board[row][column] = ‘ ‘;24          if( column > 0 && board[row][column - 1] == left.charAt(0)){//go left25              if( dfs(board, left.substring(1), row, column - 1)) return true;26          }27          if( column < width - 1 && board[row][column + 1] == left.charAt(0)){//go right28              if( dfs(board, left.substring(1), row, column + 1)) return true;29          }30          if( row > 0 && board[row - 1][column] == left.charAt(0)){//go up31              if( dfs(board, left.substring(1), row - 1, column) ) return true;32          }33          if( row < height - 1 && board[row + 1][column] == left.charAt(0)){//go down34              if( dfs(board, left.substring(1), row + 1, column)) return true;35          }36          board[row][column] = c;37          return false;38      }39 }    

In fact, this practice is not rigorous, because the question does not indicate that the string does not contain spaces. In fact, it is best to open up a tag matrix to mark which points have been matched.

In my algorithm, I made a direct judgment on the top, bottom, left, and right directions. My classmates marked the trend with an array, which is worth learning. Click here.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.