Thursday, February 26, 2015

LeetCode [79] Word Search

 79. Word Search

Medium

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 cells, where "adjacent" cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.

 

Example 1:

Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
Output: true

Example 2:

Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
Output: true

Example 3:

Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
Output: false

 

Constraints:

  • board and word consists only of lowercase and uppercase English letters.
  • 1 <= board.length <= 200
  • 1 <= board[i].length <= 200
  • 1 <= word.length <= 10^3
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
class Solution {
public:
    bool bt(vector<vector<char> > &board, string word, bool **passed, int i, int j, int m, int n, int len, int nw){
        if(len==nw) return true;
        //up
        if(i-1>=0 && board[i-1][j]==word[len] && !passed[i-1][j]){
                passed[i-1][j] = true;
                if(bt(board, word, passed, i-1, j, m, n, len+1, nw)) return true;
                passed[i-1][j] = false;
        }
        //low
        if(i+1<m && board[i+1][j]==word[len] && !passed[i+1][j]){
                passed[i+1][j] = true;
                if(bt(board, word, passed, i+1, j, m, n, len+1, nw)) return true;
                passed[i+1][j] = false;
        }   
        //left
        if(j-1>=0 && board[i][j-1]==word[len] && !passed[i][j-1]){
                passed[i][j-1] = true;
                if(bt(board, word, passed, i, j-1, m, n, len+1, nw)) return true;
                passed[i][j-1] = false;
        }       
        //left
        if(j+1<n && board[i][j+1]==word[len] && !passed[i][j+1]){
                passed[i][j+1] = true;
                if(bt(board, word, passed, i, j+1, m, n, len+1, nw)) return true;
                passed[i][j+1] = false;
        }   
        return false;
    }
    
    bool exist(vector<vector<char> > &board, string word) {
        int m = board.size();
        if(m==0) return false;
        int n = board[0].size();
        int nw = word.size();
        if(n==0||nw==0) return false;
        bool **passed = new bool *[m];
        for(int i=0; i<m; ++i){
          passed[i] = new bool[n];
        }

        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(board[i][j]==word[0]&&!passed[i][j]){
                    passed[i][j] = true;
                    if(bt(board, word, passed, i, j, m, n, 1, nw)) return true;
                    passed[i][j] = false;
                }
            }
        }
        return false;
    }
};

class Solution {
public:
    bool exist(vector<vector<char>>& board, string word) {
        if(word.empty()) return true;
        int m = board.size();
        if(m==0) return false;
        int n = board[0].size();
        if(n==0) return false;
        
        string cur;
        int sz = word.size();
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                char c = word[0];
                if(board[i][j]==c){
                    cur += c;
                    board[i][j] = '.';
                    if(bt(board, cur, word, 1, i, j, sz, m, n)) return true;
                    board[i][j] = c;
                }
            }
        }
        return false;
    }
    
    bool bt(vector<vector<char>>& board, string cur, string word, int len, int i, int j, int sz, int m, int n){
        if(len==sz) return true;
        char c = word[len];
        string cur1 = cur;
        cur1 += c;
        if(i-1>=0 && board[i-1][j]==c){
            board[i-1][j] = '.';
            if(bt(board, cur1, word, len+1, i-1, j, sz, m, n)) return true;
            board[i-1][j] = c;
        }
        if(i+1<m && board[i+1][j]==c){
            board[i+1][j] = '.';
            if(bt(board, cur1, word, len+1, i+1, j, sz, m, n)) return true;
            board[i+1][j] = c;
        }
        if(j-1>=0 && board[i][j-1]==c){
            board[i][j-1] = '.';
            if(bt(board, cur1, word, len+1, i, j-1, sz, m, n)) return true;
            board[i][j-1] = c;
        }
        if(j+1<n && board[i][j+1]==c){
            board[i][j+1] = '.';
            if(bt(board, cur1, word, len+1, i, j+1, sz, m, n)) return true;
            board[i][j+1] = c;
        }        
        return false;
    }
};

class Solution {
public:
    bool exist(vector<vector<char>>& board, string word) {
        int m = board.size();
        if(m==0) return false;
        int n = board[0].size();
        
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(dfs(board, word, 0, word.size(), i, j, m, n)) return true;
            }
        }
        return false;
    }
    
    bool dfs(vector<vector<char>>& board, string word, int pos, int sz, int i, int j, int m, int n){

            if(pos<sz && board[i][j]==word[pos]){
                if(pos+1==sz) return true;
                board[i][j]='#';
                if(i-1>=0) if(dfs(board, word, pos+1, sz, i-1, j, m, n)) return true;
                if(i+1<m)  if(dfs(board, word, pos+1, sz, i+1, j, m, n)) return true;
                if(j-1>=0) if(dfs(board, word, pos+1, sz, i, j-1, m, n)) return true;
                if(j+1<n)  if(dfs(board, word, pos+1, sz, i, j+1, m, n)) return true;
                board[i][j]=word[pos];
            }

        return false;
    }
};

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
    int m, n;
    int[][] dirs = {{-1,0},{1,0},{0,-1},{0,1}};
    public boolean exist(char[][] board, String word) {
        m = board.length;
        n = board[0].length;
        
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(board[i][j]==word.charAt(0)){
                    board[i][j] = '.';
                    if(find(board, word, 1, i, j)) return true;
                    board[i][j] = word.charAt(0);
                }
            }
        }
        return false;
    }
    
    boolean find(char[][] board, String word, int p, int i, int j){
        if(p==word.length()) return true;
        for(int[] d : dirs){
            int ii = i+d[0], jj = j+d[1];
            if(ii>=0 && ii<m && jj>=0 && jj<n && board[ii][jj]==word.charAt(p)){
                board[ii][jj] = '.';
                if(find(board, word, p+1, ii, jj)) return true;
                board[ii][jj] = word.charAt(p);
            }
        }
        return false;
    }
}

No comments:

Post a Comment