Word Search II

原创 2015年07月10日 00:13:05

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 letters a-z.

struct TrieNode
{
    bool isEnd;
    TrieNode *children[26];
    TrieNode() : isEnd(false)
    {
        for (int i = 0; i < 26; i++)
        {
            children[i] = NULL;
        }
    }
};

class Trie
{
public:
    Trie()
    {
        root = new TrieNode();
    }

    ~Trie()
    {
        deleteNode (root);
    }

    void deleteNode(TrieNode *p)
    {
        if (p == NULL)
        {
            return;
        }
        for (int i = 0; i < 26; i++)
        {
            if (p->children[i])
            {
                deleteNode(p->children[i]);
            }
        }

        delete p;
    }

    void add(string str)
    {
        TrieNode *cur = root;
        for (int i = 0; i < str.length(); i++)
        {
            int index = str[i] - 'a';
            if (cur->children[index] == NULL)
            {
                cur->children[index] = new TrieNode();
            }
            cur = cur->children[index];
        }

        cur->isEnd = true;
    }

    TrieNode *root;
};

class Solution {
public:
    vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
        vector<string> result;
        Trie tree;
        for (int i = 0; i < words.size(); i++)
        {
            tree.add(words[i]);
        }

        string buf;
        int m = board.size();
        int n = board[0].size();
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                visit(tree.root, board, m, n, i, j, buf, result);
            }
        }

        return result; 
    }
    
private:
    void visit(TrieNode *node, vector<vector<char> > &board, int m, int n,
               int row, int column, string buf, vector<string> &result)
        {
            if (node == NULL)
            {
                return;
            }

            if (board[row][column] == '*')
            {
                return;
            }

            int index = board[row][column] - 'a';
            if (node->children[index])
            {
                buf += board[row][column];
            }
            else
            {
                return;
            }
            char value = board[row][column];
            board[row][column] = '*';
            if (node->children[index]->isEnd)
            {
                node->children[index]->isEnd = false;
                result.push_back(buf);
            }

            if (column+1 < n)
            {
                visit(node->children[index], board, m, n, 
                      row, column+1, buf, result);
            }

            if (column-1 >= 0)
            {
                visit(node->children[index], board, m, n, 
                      row, column-1, buf, result);
            }

            if (row-1 >= 0)
            {
                visit(node->children[index], board, m, n,
                      row-1, column, buf, result);
            }

            if (row+1 < m)
            {
                visit(node->children[index], board, m, n, 
                      row+1, column, buf, result);
            }
            board[row][column] = value;
    }
};


[leetcode] 212. Word Search II

Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...

[leetcde 212, Hard] Word Search II

[leetcde 212, Hard] Word Search II

leetcode 212. Word Search II

trie + backtracking class Solution { public: vector findWords(vector>& board, vector& words) { Tr...

leetcode Word Search II

题目Given a 2D board and a list of words from the dictionary, find all words in the board.Each word mu...

LeetCode-Word Search II-解题报告

原题链接https://leetcode.com/problems/word-search-ii/ Given a 2D board and a list of words from t...

LeetCode 212 Word Search II (Trie树+DFS)

LeetCode 212 Word Search II (Trie树+DFS)

LeetCode - Word Search II

Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...
  • fenx91
  • fenx91
  • 2015年08月03日 14:50
  • 173

【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连

maxtree: http://lintcode.com/submission/60239/ 九章算法中有讲到, 对每个节点,找到离他最近且比它大的左右两个节点中较小的那个,作为他的父节点。 其中,...

212. Word Search II

Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mu...

[LeetCode212] Word Search II

HARDGiven a 2D board and a list of words from the dictionary, find all words in the board.Each word ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Word Search II
举报原因:
原因补充:

(最多只允许输入30个字)