[LeetCode] 212. Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board.
Each word must 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 in a word.
For example,
Given words =
Given words =
["oath","pea","eat","rain"]
and board =[ ['o','a','a','n'], ['e','t','a','e'], ['i','h','k','r'], ['i','f','l','v'] ]Return
["eat","oath"]
.
Note:
You may assume that all inputs are consist of lowercase letters
You may assume that all inputs are consist of lowercase letters
a-z
.
You would need to optimize your backtracking to pass the larger test. Could you stop backtracking earlier?
If the current candidate does not exist in all words' prefix, you could stop backtracking immediately. What kind of data structure could answer such query efficiently? Does a hash table work? Why or why not? How about a Trie? If you would like to learn how to implement a basic trie, please work on this problem: Implement Trie (Prefix Tree) first.
Thought process:
Add all words into a trie. For every character on board, do a DFS on it. If current word is not a prefix in the trie, return. If current word is a complete word in the trie, I have found a word.
Solution:
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 | class Solution { class TrieNode { TrieNode[] children; boolean isWord; TrieNode() { children = new TrieNode[26]; } } private TrieNode root = new TrieNode(); private int[][] directions = { { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 } }; public List<String> findWords(char[][] board, String[] words) { buildTrie(words); List<String> list = new ArrayList<>(); for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { findWords(board, i, j, "" + board[i][j], root.children[board[i][j] - 'a'], list); } } return list; } private void buildTrie(String[] words) { for (String word : words) { TrieNode current = root; for (char c : word.toCharArray()) { int index = c - 'a'; if (current.children[index] == null) { current.children[index] = new TrieNode(); } current = current.children[index]; } current.isWord = true; } } private void findWords(char[][] board, int row, int col, String current, TrieNode node, List<String> list) { if (node == null) { return; } if (node.isWord && !list.contains(current)) { list.add(current); } char temp = board[row][col]; board[row][col] = ' '; for (int i = 0; i < 4; i++) { int nextRow = row + directions[i][0]; int nextCol = col + directions[i][1]; if (nextRow >= 0 && nextRow < board.length && nextCol >= 0 && nextCol < board[0].length) { char c = board[nextRow][nextCol]; if (c == ' ') { continue; } findWords(board, nextRow, nextCol, current + c, node.children[c - 'a'], list); } } board[row][col] = temp; } } |
Time complexity:
Say the height of board is a, width of board is b, size of dictionary is c, and the average length of words is d. Building trie takes O(cd). Finding words takes O(abd). The overall time complexity is O((ab + c)d).
Say the height of board is a, width of board is b, size of dictionary is c, and the average length of words is d. Building trie takes O(cd). Finding words takes O(abd). The overall time complexity is O((ab + c)d).
Comments
Post a Comment