LeetCode Word Search II DFS+Trie树

思路:

参考DISCUSS,使用Trie树加快dfs的搜索速度。

java code:

public class Solution {

    //construct a Trie to speed up search process
    class TrieNode {
        // Initialize your data structure here.
        public boolean isWord = false;
        public TrieNode[] children = new TrieNode[26];
        public TrieNode() {

        }
    }
    public class Trie {
        private TrieNode root;

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

        // Inserts a word into the trie.
        public void insert(String word) {
            TrieNode cur = root;
            for(int i = 0; i < word.length(); ++i) {
                char c = word.charAt(i);
                if(cur.children[c - 'a'] == null) {
                    cur.children[c - 'a'] = new TrieNode();
                }
                cur = cur.children[c - 'a'];
            }
            cur.isWord = true;
        }

        // Returns if the word is in the trie.
        public boolean search(String word) {
            TrieNode cur = root;
            for(int i = 0; i < word.length(); ++i) {
                char c = word.charAt(i);
                if(cur.children[c - 'a'] == null) return false;
                cur = cur.children[c - 'a'];
            }
            return cur.isWord;
        }

        // Returns if there is any word in the trie
        // that starts with the given prefix.
        public boolean startsWith(String prefix) {
            TrieNode cur = root;
            for(int i = 0; i < prefix.length(); ++i) {
                char c = prefix.charAt(i);
                if(cur.children[c - 'a'] == null) return false;
                cur = cur.children[c - 'a'];
            }
            return true;
        }
    }

    Set<String> res = new HashSet<String>();
    public List<String> findWords(char[][] board, String[] words) {
        //construct a Trie
        Trie trie = new Trie();
        //init Tire
        for(int i = 0; i < words.length; ++i) {
            trie.insert(words[i]);
        }
        int m = board.length;
        int n = board[0].length;
        boolean[][] visited = new boolean[m][n];
        //start search from every character
        for(int i = 0; i < m; ++i) {
            for(int j = 0; j < n; ++j) {
                dfs(board, visited, "", i, j, trie);
            }
        }
        return new ArrayList<String>(res);
    }

    public void dfs(char[][] board, boolean[][] visited, String curStr, int i, int j, Trie trie) {
        if(i < 0 || i >= board.length || j < 0 || j >= board[0].length) return;
        if(visited[i][j] == true) return;

        curStr += board[i][j];
        //if Trie doestn't have curStr, it must not have the word which start with curStr, so pass
        if(!trie.startsWith(curStr)) return;
        if(trie.search(curStr)) {
            res.add(curStr);
        }

        visited[i][j] = true;
        dfs(board, visited, curStr, i + 1, j, trie);
        dfs(board, visited, curStr, i - 1, j, trie);
        dfs(board, visited, curStr, i, j + 1, trie);
        dfs(board, visited, curStr, i, j - 1, trie);
        visited[i][j] = false;
    }
}
    原文作者:Trie树
    原文地址: https://blog.csdn.net/yeruby/article/details/49681627
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞