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 =["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 lettersa-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.
Simple use a DFS is a waste of time, use a prefix tree(trie tree) instead.
Basically, a trie tree provides three interfaces: insert, search and startsWith.
To eliminate duplicates in words, I always set the count variable to 1 rather than count++ in my implementation of prefix tree.
Tips in writing DFS:
Always record the cells you have VISITED, and do not touch them again. Both a hash table or modify the content to a sentinel mark will work.
Do REMEMBER to erase word that have been founded, or it will cost duplicates results.
class TrieNode { public: TrieNode * child[26]; int count; TrieNode(){ for(int i = 0; i < 26; i++){ child[i] = nullptr; } count = 0; } }; class Trie{ private: TrieNode * root; public: Trie(){ root = new TrieNode(); } void insert(string word){ TrieNode * p = root; for(char c : word){ int i = c - 'a'; if(p->child[i] == nullptr){ p->child[i] = new TrieNode(); } p = p->child[i]; } p->count = 1; } bool search(string word){ TrieNode * p = root; for(char c : word){ int i = c - 'a'; if(p->child[i] == nullptr){ return false; } p = p->child[i]; } return p->count > 0; } bool startsWith(string prefix){ TrieNode * p = root; for(char c : prefix){ int i = c - 'a'; if(p->child[i] == nullptr){ return false; } p = p->child[i]; } return true; } void erase(string word){ stack<TrieNode*> s; TrieNode * p = root; for(char c : word){ int i = c - 'a'; s.push(p); p =p->child[i]; } p->count--; s.push(p); TrieNode * child = nullptr; while(!s.empty()){ TrieNode * p = s.top(); s.pop(); if(child != nullptr){ for(int i = 0; i < 26; i++){ if(child == p->child[i]){ delete p->child[i]; p->child[i] = nullptr; break; } } } if(p->count > 0){ return ; } for(int i = 0; i < 26; i++){ if(p->child[i] != nullptr){ return ; } } child = p; } } }; class Solution { public: vector<string> findWords(vector<vector<char>>& board, vector<string>& words) { vector<string> ans; string thisAns; Trie trie; if(board.size() == 0 || board[0].size() == 0) return ans; for(auto word : words){ trie.insert(word); } for(int i = 0; i < board.size(); i++){ for(int j = 0; j < board[0].size(); j++){ dfs(board, i, j, thisAns, ans, trie); } } return ans; } void dfs(vector<vector<char> >& board, int r, int c, string& thisAns, vector<string>& ans, Trie & trie){ int height = board.size(); int width = board[0].size(); int direct[][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; if(r < 0 || c < 0 || r >= height || c >= width){ return ;//out of index } char character = board[r][c]; if(character == '0'){ return ;//visited } thisAns.push_back(character); board[r][c] = '0'; if(trie.search(thisAns) == true){ ans.push_back(thisAns); trie.erase(thisAns);//THIS LINE IS IMPORTENT } if(trie.startsWith(thisAns) == true){ for(int i = 0; i < 4; i++){ int nr = r + direct[i][0]; int nc = c + direct[i][1]; dfs(board, nr, nc, thisAns, ans, trie); } } thisAns.pop_back(); board[r][c] = character; } };