Wednesday, October 4, 2017

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,
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.


/* method 1: 
 * time:O(m*n*4^L), space:O(m*n), m: board's row, n: board's column, L: word size
 * Imagine this is a tree,  this quadtree's height is L(L is length of word) and its total node number is
 * 4^0 + 4^1 + ... + 4^L = 1/3 * ( 4^(L+1) - 1 ). So the time complexity of this dfs solution is  * mnO(4^L).
 */
public class Solution {
    public boolean exist(char[][] board, String word) {
        char[] words = word.toCharArray();
        int width = board.length;
        int length = board[0].length;
        boolean[][] visited = new boolean[width][length];
        for (int y = 0; y < width; y++) {
        for (int x = 0; x < length; x++) {
        if (exist(board, y, x, words, 0, visited)) {
                                return true;
                        }
        }
        }
        return false;
    }
    
    private boolean exist(char[][] board, int y, int x, char[] words, int i, boolean[][] visited) {
    if (i == words.length) {
            return true;
        }
    if (y < 0 || x < 0 || y == board.length || x == board[y].length || visited[y][x] == true) {
            return false;
        }
    if (board[y][x] != words[i]) {
            return false;
        }
    visited[y][x] = true;
    boolean exist = exist(board, y, x + 1, words, i + 1, visited)
               || exist(board, y, x - 1, words, i + 1, visited)
               || exist(board, y + 1, x, words, i + 1, visited)
               || exist(board, y - 1, x, words, i + 1, visited);
    visited[y][x] = false;
    return exist;
    }

}



/* method2:
 time:O(m*n*4^L), space:O(1)
 *
 * board[y][x] ^= 256 it's a marker that the letter at position x,y is a part of word we search.
 * After board[y][x] ^= 256 the char became not a valid letter. After second board[y][x] ^= 256
 * it became a valid letter again.
 */

public class Solution {
    public boolean exist(char[][] board, String word) {
        char[] words = word.toCharArray();
        int row = board.length;
        int column = board[0].length;
        for (int y = 0; y < row; y++) {
         for (int x = 0; x < column; x++) {
         if (exist(board, y, x, words, 0)) {
                              return true;
                        }
         }
        }
        return false;
    }
    
    private boolean exist(char[][] board, int y, int x, char[] words, int i) {
     if (i == words.length) {
            return true;
        }
     if (y < 0 || x < 0 || y == board.length || x == board[y].length) {
            return false;
        }
     if (board[y][x] != words[i]) {
            return false;
        }
     board[y][x] ^= 256;
     boolean exist = exist(board, y, x + 1, words, i + 1)
                 || exist(board, y, x - 1, words, i + 1)
                 || exist(board, y + 1, x, words, i + 1)
                 || exist(board, y - 1, x, words, i + 1);
     board[y][x] ^= 256;
     return exist;
    }

}

No comments:

Post a Comment