LeetCode - Word Search
2014-02-27 00:03
218 查看
Word Search
2014.2.26 23:59
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 =
word =
word =
word =
Solution:
Simple problem of DFS.
Total time complexity is O(n^2 * len(word)). Space complexity is O(n^2).
Accepted code:
2014.2.26 23:59
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 =
[ ["ABCE"], ["SFCS"], ["ADEE"] ]
word =
"ABCCED", -> returns
true,
word =
"SEE", -> returns
true,
word =
"ABCB", -> returns
false.
Solution:
Simple problem of DFS.
Total time complexity is O(n^2 * len(word)). Space complexity is O(n^2).
Accepted code:
// 1CE, 1AC, very smooth. class Solution { public: bool exist(vector<vector<char> > &board, string word) { n = (int)board.size(); if (n == 0) { return false; } m = (int)board[0].size(); word_len = (int)word.length(); if (word_len == 0) { return true; } int i, j; for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) { if(dfs(board, word, i, j, 0)) { return true; } } } return false; } private: int n, m; int word_len; bool dfs(vector<vector<char> > &board, string &word, int x, int y, int idx) { if (x < 0 || x > n - 1 || y < 0 || y > m - 1) { return false; } if (board[x][y] < 'A' || board[x][y] != word[idx]) { // already searched here // letter mismatch here return false; } bool res; if (idx == word_len - 1) { // reach the end of word, success return true; } else { // up board[x][y] -= 'A'; res = dfs(board, word, x - 1, y, idx + 1); board[x][y] += 'A'; if (res) { return true; } // down board[x][y] -= 'A'; res = dfs(board, word, x + 1, y, idx + 1); board[x][y] += 'A'; if (res) { return true; } // left board[x][y] -= 'A'; res = dfs(board, word, x, y - 1, idx + 1); board[x][y] += 'A'; if (res) { return true; } // right board[x][y] -= 'A'; res = dfs(board, word, x, y + 1, idx + 1); board[x][y] += 'A'; if (res) { return true; } } // all letters will be within [A-Z], thus I marked a position as 'searched' by setting them to an invalid value. // we have to restore the value when the DFS is done, so their values must still be distiguishable. // therefore, I used an offset value of 'A'. // this tricky way is to save the extra O(n * m) space needed as marker array. return false; } };
相关文章推荐
- LeetCode —— Word Search
- leetcode,79. Word Search
- Leetcode -- Word Search
- [Leetcode 69] 79 Word Search
- Word Search - Leetcode
- leetcode-Word Search
- [leetcode] 79. Word Search 解题报告
- [Leetcode] #79 Word Search
- 【leetcode】Word Search
- Word Search - leetcode
- LeetCode *** 79. Word Search
- [LeetCode] Word Search [37]
- [Leetcode] Word Search
- [LeetCode][JavaScript]Word Search
- [LeetCode] Word Search [37]
- 【LeetCode】Word Search
- [LeetCode 79] Word Search
- [LeetCode] Word Search
- [LeetCode]Word Search
- Word Search(leetcode)