473. Add and Search Word - Data structure design

  1. class TrieNode {
  2. public:
  3. TrieNode * next[26];
  4. bool isWord;
  5. TrieNode() {
  6. for (int i = 0; i < 26; i++) {
  7. next[i] = NULL;
  8. }
  9. isWord = false;
  10. }
  11. };
  12. class WordDictionary {
  13. private:
  14. TrieNode * root;
  15. public:
  16. /*
  17. * @param word: Adds a word into the data structure.
  18. * @return: nothing
  19. */
  20. WordDictionary() {
  21. root = new TrieNode;
  22. }
  23. void addWord(string &word) {
  24. // write your code here
  25. TrieNode * temp = root;
  26. for (int i = 0; i < word.size(); i++) {
  27. if (temp->next[word[i] - 'a'] == NULL) {
  28. temp->next[word[i] - 'a'] = new TrieNode();
  29. }
  30. temp = temp->next[word[i] - 'a'];
  31. }
  32. temp -> isWord = true;
  33. }
  34. /*
  35. * @param word: A word could contain the dot character '.' to represent any one letter.
  36. * @return: if the word is in the data structure.
  37. */
  38. bool search(string &word) {
  39. return search(word, word.size(), 0, root);
  40. }
  41. bool search(string &word, int n, int pos, TrieNode * cur) {
  42. // write your code here
  43. if (cur == NULL) {
  44. return false;
  45. }
  46. if (pos == n) {
  47. return cur -> isWord;
  48. }
  49. if (word[pos] == '.') {
  50. for (int j = 0; j < 26; j++) {
  51. if (search(word, n, pos + 1, cur->next[j]) == true) {
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. else {
  58. //if (cur -> next[word[pos] - 'a'])
  59. return search(word, n, pos + 1, cur->next[word[pos] - 'a']);
  60. }
  61. }
  62. };

132. Word Search II

class TrieNode {
public:
    TrieNode * next[26];
    bool isWord;
    TrieNode() {
        for (int i = 0; i < 26; i++) {
            next[i] = NULL;
        }
        isWord = false;
    }
};
class Solution {
private:
    TrieNode * root;
    vector <string> res;
    vector <int> row = { -1, 0, 1, 0 };
    vector <int> column = { 0, 1, 0, -1 };
public:
    /**
    * @param board: A list of lists of character
    * @param words: A list of string
    * @return: A list of string
    */
    Solution(){
        root = new TrieNode();
    }
    vector<string> wordSearchII(vector<vector<char>> &board, vector<string> &words) {
        // write your code here
        for (int i = 0; i < words.size(); i++) {
            TrieNode * temp;
            temp = root;
            for (int j = 0; j < words[i].size(); j++) {
                if (temp->next[words[i][j] - 'a'] == NULL) {
                    temp->next[words[i][j] - 'a'] = new TrieNode();
                }
                temp = temp->next[words[i][j] - 'a'];
            }
            temp->isWord = true;
        }
        cout << 'a';
        string toNow;
        vector <vector<int>> visited(board.size(), vector<int>(board[0].size()));
        for (int i = 0; i < board.size(); i++) {
            for (int j = 0; j < board[0].size(); j++) {
                if (root->next[board[i][j] - 'a'] != NULL){
                    toNow.push_back(board[i][j]);
                    visited[i][j] = 1;
                    helper(board, words, i, j, root->next[board[i][j] - 'a'], toNow, visited);
                    visited[i][j] = 0;
                    toNow.pop_back();
                }
            }
        }
        return res;

    }
    bool exist(vector<vector<char>> &board, int i, int j, vector<vector<int>> &visited) {

        if (i < 0 || i >= board.size()) {
            return false;
        }
        if (j < 0 || j >= board[0].size()) {
            return false;
        }
        if (visited[i][j] == 1)
            return false;
        return true;
    }
    void helper(vector<vector<char>> &board, vector<string> &words, int rowNow, int colNow, TrieNode * temp, string & toNow, vector<vector<int>> &visited) {
        if (temp->isWord == true) {
            vector <string> :: iterator it = find(res.begin(), res.end(), toNow);
            if (it == res.end())
                res.push_back(toNow);
        }
        for (int i = 0; i < 4; i++) {
            int nextRow = rowNow + row[i];
            int nextCol = colNow + column[i];
            if (exist(board, nextRow, nextCol, visited) && temp->next[board[nextRow][nextCol] - 'a'] != NULL) {
                toNow.push_back(board[nextRow][nextCol]);
                visited[nextRow][nextCol] = 1;
                helper(board, words, nextRow, nextCol, temp->next[board[nextRow][nextCol] - 'a'], toNow, visited);
                visited[nextRow][nextCol] = 0;
                toNow.pop_back();
            }
        }
        return;

    }
};