Skip to content

Instantly share code, notes, and snippets.

@hsaputra
Last active October 26, 2018 06:47
Show Gist options
  • Save hsaputra/8ee17b0bfa274f42658d226555f9db21 to your computer and use it in GitHub Desktop.
Save hsaputra/8ee17b0bfa274f42658d226555f9db21 to your computer and use it in GitHub Desktop.
/**
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.
Example:
board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
Given word = "ABCCED", return true.
Given word = "SEE", return true.
Given word = "ABCB", return false.
**/
class Solution {
public boolean exist(char[][] board, String word) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
boolean[][] visited = new boolean[board.length][board[0].length];
if (search(board, word, 0, i, j, visited)) {
return true;
}
}
}
return false;
}
private boolean search(char[][] board, String word, int word_i, int i, int j, boolean[][] visited) {
if (word_i == word.length()) {
return true;
}
if (i < 0 || j < 0 || i >= board.length || j >= board[i].length) {
return false;
}
if (visited[i][j]) {
return false;
}
if (word.charAt(word_i) != board[i][j]) {
return false;
}
visited[i][j] = true;
boolean existed =
search(board, word, word_i + 1, i + 1, j, visited) ||
search(board, word, word_i + 1, i - 1, j, visited) ||
search(board, word, word_i + 1, i, j + 1, visited) ||
search(board, word, word_i + 1, i, j - 1, visited);
visited[i][j] = false;
return existed;
}
}
@hsaputra
Copy link
Author

hsaputra commented Oct 26, 2018

class Solution {
    public boolean exist(char[][] board, String word) {
      // test input 
      if (board == null || board.length == 0 || board[0].length == 0) {
        return false;
      }
      
      if (word == null || word.length() == 0) {
        return false;
      }
      
      int rows = board.length;
      int cols = board[0].length;
      boolean[][] isValid = new boolean[rows][cols];
      
      for (int i = 0; i < board.length; i ++) {
        for (int j = 0; j < board[i].length; j++) {
          boolean isFound = recursiveWordSearch(board, isValid, word, 0, i, j);
          if (isFound) {
            return isFound;
          }
        }
      }
      
      return false;
    }
  
    private boolean recursiveWordSearch(final char[][] board, final boolean[][] isValid, 
                                        String word, int index, int i, int j) {
      
      // stopping condition
      if (index >= word.length()) {
        return true;
      }
      
      // stopping condition - out of bound
      if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) {
        return false;
      }
      
      // check conditions - used char
      boolean isCurVisited = isValid[i][j];
      if (isCurVisited) {
        return false;
      }
      
      // get cur char
      char curChar = word.charAt(index);
      char charBoard = board[i][j];
      
      boolean wordFound = false;
      if (curChar == charBoard) {
        isValid[i][j] = true;
        wordFound = recursiveWordSearch(board, isValid, word, index + 1, i, j-1) ||
                            recursiveWordSearch(board, isValid, word, index + 1, i, j+1) ||
                            recursiveWordSearch(board, isValid, word, index + 1, i-1, j) ||
                            recursiveWordSearch(board, isValid, word, index + 1, i+1, j);
                            
        isValid[i][j] = false;
      }
      
      return wordFound;
      
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment