|
| 1 | +struct Node { |
| 2 | + Node(char ch):character(ch) { } |
| 3 | + char character; |
| 4 | + vector<Node*> next; |
| 5 | +}; |
| 6 | + |
| 7 | +const char endChar = 1; |
| 8 | +class Trie { |
| 9 | +public: |
| 10 | + /** Initialize your data structure here. */ |
| 11 | + Trie() { |
| 12 | + head = new Node(endChar); |
| 13 | + } |
| 14 | + |
| 15 | + ~Trie() { |
| 16 | + del(head); |
| 17 | + } |
| 18 | + |
| 19 | + /** Inserts a word into the trie. */ |
| 20 | + void insert(string word) { |
| 21 | + word += endChar; |
| 22 | + Node *p = head; |
| 23 | + for (auto&& ch : word) { |
| 24 | + int index=0; |
| 25 | + for (;index < p->next.size();++index) { |
| 26 | + if (p->next[index]->character == ch) break; |
| 27 | + } |
| 28 | + if (index != p->next.size()) { |
| 29 | + p = p->next[index]; |
| 30 | + } else { |
| 31 | + p->next.push_back(new Node(ch)); |
| 32 | + p = p->next[p->next.size()-1]; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + Node* getNext(char c, Node *p) { |
| 38 | + int index=0; |
| 39 | + for (;index < p->next.size();++index) { |
| 40 | + if (p->next[index]->character == c) return p->next[index]; |
| 41 | + } |
| 42 | + return nullptr; |
| 43 | + } |
| 44 | + |
| 45 | + inline Node* getHead() { |
| 46 | + return head; |
| 47 | + } |
| 48 | + |
| 49 | +private: |
| 50 | + Node *head; |
| 51 | + |
| 52 | + void del(Node *h) { |
| 53 | + for (auto&& p : h->next) { |
| 54 | + del(p); |
| 55 | + } |
| 56 | + delete h; |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | + |
| 61 | +class Solution { |
| 62 | +public: |
| 63 | + vector<string> findWords(vector<vector<char>>& board, vector<string>& words) { |
| 64 | + trie = new Trie(); |
| 65 | + for (auto&& word : words) { |
| 66 | + trie->insert(word); |
| 67 | + } |
| 68 | + set<string> ans; |
| 69 | + Node *head = trie->getHead(); |
| 70 | + for (int x = 0; x < board.size(); ++x) { |
| 71 | + for (int y = 0; y < board[0].size(); ++y) { |
| 72 | + _findWords(board, x, y, head, "", ans); |
| 73 | + } |
| 74 | + } |
| 75 | + vector<string> actual_ans; |
| 76 | + for (auto&& a : ans) { |
| 77 | + actual_ans.push_back(a); |
| 78 | + } |
| 79 | + return actual_ans; |
| 80 | + } |
| 81 | + |
| 82 | +private: |
| 83 | + Trie *trie; |
| 84 | + void _findWords(vector<vector<char>> &board, int x, int y, Node* p, string word, set<string> &ans) { |
| 85 | + if (x < 0 || x >= board.size() || y < 0 || y >= board[0].size() || board[x][y] == 0) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + if ((p=trie->getNext(board[x][y], p))) { |
| 90 | + word += board[x][y]; |
| 91 | + if (trie->getNext(endChar, p)) { |
| 92 | + ans.insert(word); |
| 93 | + } |
| 94 | + |
| 95 | + char c = board[x][y]; |
| 96 | + board[x][y] = 0; |
| 97 | + _findWords(board, x+1, y, p, word, ans); |
| 98 | + _findWords(board, x-1, y, p, word, ans); |
| 99 | + _findWords(board, x, y-1, p, word, ans); |
| 100 | + _findWords(board, x, y+1, p, word, ans); |
| 101 | + board[x][y] = c; |
| 102 | + } |
| 103 | + } |
| 104 | +}; |
0 commit comments