| 
 | 1 | +/*  | 
 | 2 | + * Given a 2D board and list of words from a dictionary,  | 
 | 3 | + * find all the possible words on board fromt the list.  | 
 | 4 | + * Example:  | 
 | 5 | + * words = ["oath","pea","eat","rain"]  | 
 | 6 | + * board =   | 
 | 7 | + * [  | 
 | 8 | + *  ['o','a','a','n'],  | 
 | 9 | + *  ['e','t','a','e'],  | 
 | 10 | + *  ['i','h','k','r'],  | 
 | 11 | + *  ['i','f','l','v']  | 
 | 12 | + * ]  | 
 | 13 | + *   | 
 | 14 | + * Output: ["eat", "oath"]  | 
 | 15 | + *   | 
 | 16 | + * Source : LeetCode Problem 212  | 
 | 17 | + *   | 
 | 18 | + * Approach:  | 
 | 19 | + *   | 
 | 20 | + * We can use backtracking(dfs) to traverse from each character on board  | 
 | 21 | + * to all four direction. In order to do a fast searching, we can use  | 
 | 22 | + * a trie from words in dictionary, as soon as we find a word from dictionary  | 
 | 23 | + * in trie, we remove it from trie, so we don't have duplicates.  | 
 | 24 | + * Also, we can use the board to mark '#' as visited character to   | 
 | 25 | + * achieve backtracking.  | 
 | 26 | + */  | 
 | 27 | + | 
 | 28 | +#include <vector>  | 
 | 29 | +#include <iostream>  | 
 | 30 | + | 
 | 31 | +struct TrieNode {  | 
 | 32 | +    std::string word;  | 
 | 33 | +    std::vector<TrieNode*> children;  | 
 | 34 | +    TrieNode():  | 
 | 35 | +        word{""}, children{std::vector<TrieNode*>(26, nullptr)}{}  | 
 | 36 | +};  | 
 | 37 | + | 
 | 38 | +TrieNode* build_trie(const std::vector<std::string>& dictionary)  | 
 | 39 | +{  | 
 | 40 | +    TrieNode* root = new TrieNode();  | 
 | 41 | +    for (auto word: dictionary) {  | 
 | 42 | +        TrieNode* curr = root;  | 
 | 43 | +        for (auto c: word) {  | 
 | 44 | +            int index = c - 'a';  | 
 | 45 | +            if (curr->children[index] == nullptr) {  | 
 | 46 | +                curr->children[index] = new TrieNode();  | 
 | 47 | +            }  | 
 | 48 | +            curr = curr->children[index];  | 
 | 49 | +        }  | 
 | 50 | +        curr->word = word;  | 
 | 51 | +    }  | 
 | 52 | +    return root;  | 
 | 53 | +}  | 
 | 54 | + | 
 | 55 | +void backtrack(std::vector<std::vector<char>>& board,   | 
 | 56 | +    TrieNode* root, int i, int j, std::vector<std::string>& result)  | 
 | 57 | +{  | 
 | 58 | +    char c = board[i][j];  | 
 | 59 | +    if (c == '#' || root == nullptr) {  | 
 | 60 | +        return;  | 
 | 61 | +    }  | 
 | 62 | +    int index = c - 'a';  | 
 | 63 | +    TrieNode* curr = root->children[index];  | 
 | 64 | +    if (curr == nullptr) {  | 
 | 65 | +        return;  | 
 | 66 | +    }  | 
 | 67 | + | 
 | 68 | +    if (curr->word != "") {  | 
 | 69 | +        result.push_back(curr->word);  | 
 | 70 | +        // Reset to avoid duplicates.  | 
 | 71 | +        curr->word = "";  | 
 | 72 | +    }  | 
 | 73 | + | 
 | 74 | +    board[i][j] = '#';  | 
 | 75 | +    if (i > 0) {  | 
 | 76 | +        backtrack(board, curr, i - 1, j, result);  | 
 | 77 | +    }  | 
 | 78 | +    if (j > 0) {  | 
 | 79 | +        backtrack(board, curr, i, j - 1, result);  | 
 | 80 | +    }  | 
 | 81 | +    if (i < board.size() - 1) {  | 
 | 82 | +        backtrack(board, curr, i + 1, j, result);  | 
 | 83 | +    }  | 
 | 84 | +    if (j < board[0].size() - 1) {  | 
 | 85 | +        backtrack(board, curr, i, j + 1, result);  | 
 | 86 | +    }  | 
 | 87 | +    board[i][j] = c;  | 
 | 88 | +}  | 
 | 89 | + | 
 | 90 | +std::vector<std::string> find_words(std::vector<std::vector<char>>& board,  | 
 | 91 | +    std::vector<std::string>& dictionary)  | 
 | 92 | +{  | 
 | 93 | +    std::vector<std::string> result;  | 
 | 94 | +    for (int i = 0; i < board.size(); ++i) {  | 
 | 95 | +        for (int j = 0; j < board[0].size(); ++j) {  | 
 | 96 | +            TrieNode* root = build_trie(dictionary);  | 
 | 97 | +            backtrack(board, root, i, j, result);  | 
 | 98 | +        }  | 
 | 99 | +    }  | 
 | 100 | +    return result;  | 
 | 101 | +}  | 
 | 102 | + | 
 | 103 | +template <typename T>  | 
 | 104 | +void print_vector(const std::vector<T>& vec)  | 
 | 105 | +{  | 
 | 106 | +    std::cout << "{ ";  | 
 | 107 | +    for (auto t : vec) {  | 
 | 108 | +        std::cout << t << " ";  | 
 | 109 | +    }  | 
 | 110 | +    std::cout << " }" << std::endl;  | 
 | 111 | +}  | 
 | 112 | + | 
 | 113 | +void print_board(const std::vector<std::vector<char>>& board)  | 
 | 114 | +{  | 
 | 115 | +    for (auto v: board) {  | 
 | 116 | +        print_vector(v);  | 
 | 117 | +    }  | 
 | 118 | +}  | 
 | 119 | + | 
 | 120 | + | 
 | 121 | +int main()  | 
 | 122 | +{  | 
 | 123 | +    std::vector<std::vector<char>> board = {  | 
 | 124 | +        {'o','a','a','n'},  | 
 | 125 | +        {'e','t','a','e'},  | 
 | 126 | +        {'i','h','k','r'},  | 
 | 127 | +        {'i','f','l','v'}  | 
 | 128 | +    };  | 
 | 129 | + | 
 | 130 | +    std::vector<std::string> dictionary = {  | 
 | 131 | +        "oath","pea","eat","rain"  | 
 | 132 | +    };  | 
 | 133 | + | 
 | 134 | +    std::vector<std::string> result = find_words(board, dictionary);  | 
 | 135 | +    std::cout << "Board:" << std::endl;  | 
 | 136 | +    print_board(board);  | 
 | 137 | +    std::cout << "Dictionary:" << std::endl;  | 
 | 138 | +    print_vector(dictionary);  | 
 | 139 | +    std::cout << "Result:" << std::endl;  | 
 | 140 | +    print_vector(result);  | 
 | 141 | +    return 0;  | 
 | 142 | +}  | 
0 commit comments