单词搜索 II

给出一个由小写字母组成的矩阵和一个字典。找出所有同时在字典和矩阵中出现的单词。一个单词可以从矩阵中的任意位置开始,可以向左/右/上/下四个相邻方向移动。

样例

给出矩阵:

doaf
agai
dcan

和字典:

{"dog", "dad", "dgdg", "can", "again"}

返回 {"dog", "dad", "can", "again"}


dog:
doaf agai dcan

dad:

doaf agai dcan

can:

doaf
agai
dcan

again:

doaf
agai dcan

挑战 使用单词查找树来实现你的算法

class Node
{
public:
    char value;
    Node *children[26];
    bool isEnd;
    Node(char v) : value(v), isEnd(false)
    {
        for (int i = 0; i < 26; i++)
        {
            children[i] = NULL;
        }
    }
};


class Solution {
public:
    /**
     * @param board: A list of lists of character
     * @param words: A list of string
     * @return: A list of string
     */
    vector<string> wordSearchII(vector<vector<char> > &board, vector<string> &words) {
        // write your code here
        Node *root = new Node('r');
        for (int i = 0; i < words.size(); i++)
        {
            addToTree(root, words[i]);
        }

        set<string> result;

        int m = board.size();
        int n = board[0].size();
        vector<vector<bool> > flags(m, vector<bool>(n, false));

        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                vector<char> buf;
                findWord(root, board, m, n, i, j, buf, result, flags);
            }
        }

        vector<string> rst;

        for (set<string>::iterator it = result.begin(); it != result.end(); it++)
        {
            rst.insert(rst.begin(), *it);
        }
        return rst;
    }
private:
    void addToTree(Node *root, string &word)
    {
        Node *p = root;
        for (int i = 0; i < word.length(); i++)
        {
            if (p->children[word[i]-'a'] == NULL)
            {
                Node *child = new Node(word[i]);
                p->children[word[i]-'a'] = child;
                p = child;
            }
            else
            {
                p = p->children[word[i]-'a'];
            }
        }

        p->isEnd = true;
    }

    void findWord(Node *root, vector<vector<char> > &board, int m, int n, 
              int row, int col, vector<char> &buf, set<string> &result, 
              vector<vector<bool> > &flags)
    {
        if (row >= m || col >= n || row < 0 || col < 0)
        {
            return;
        }

        Node *p = root->children[board[row][col]-'a'];
        if (p == NULL)
        {
            return;
        }
        if (flags[row][col])
        {
            return;
        }

        buf.push_back(board[row][col]);
        flags[row][col] = true;
        if (root->children[board[row][col]-'a']->isEnd)
        {
            string temp;
            for (int i = 0; i < buf.size(); i++)
            {
                temp += buf[i];
            }
            result.insert(temp);
        }
        findWord(p, board, m, n, row, col-1, buf, result, flags);
        findWord(p, board, m, n, row, col+1, buf, result, flags);
        findWord(p, board, m, n, row-1, col, buf, result, flags);
        findWord(p, board, m, n, row+1, col, buf, result, flags);
        buf.pop_back();
        flags[row][col] = false;
    }
};



你可能感兴趣的:(单词搜索 II)