您的位置:首页 > 其它

212. Word Search II

2017-03-06 11:58 369 查看
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
.

Solution:

Using Dfs can generate all the possible words that the board could have, but it takes to much time and has unnecessary computation.  for example if we have search "oaa" in the board above, we could stop search since no word in the words have prefix "oaa". 

How do we decide if there is no word in the words array that have the prefix "xxxx" ?  We build a trie.

Code: 

public class Solution {
    int[] dirX = {1,-1,0,0};
    int[] dirY = {0,0,1,-1};
    
    public List<String> findWords(char[][] board, String[] words) {
        if(board.length == 0 || board[0].length == 0) return new ArrayList<String>();
        TrieNode root = buildTrie(words);
        HashSet<String> ret = new HashSet<>();
        boolean[][] visited = new boolean[board.length][board[0].length];
        for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board[0].length; j++){
                dfs(board,visited, ret, i, j, root);
            }
        }
        
        return new ArrayList<String>(ret);
        
    }
    
    
    public void dfs(char[][] board, boolean[][] visited, HashSet<String> ret, int i, int j, TrieNode t){
        if(i < 0 || j < 0 || i >= board.length || j >= board[0].length 
                    || visited[i][j] || t.subNodes[board[i][j] - 'a'] == null){
            return;
        }
        t = t.subNodes[board[i][j] - 'a'];
        if(t.isEnd){
            ret.add(t.s);
        }
        
        visited[i][j] = true;
        
        for(int k = 0; k < 4; k++){
            int x = i + dirX[k];
            int y = j + dirY[k];
            dfs(board,visited,ret,x,y,t);
        }
        visited[i][j] = false;
        
    }
    
    
    public TrieNode buildTrie(String[] words){
        TrieNode root = new TrieNode();
        for(String s : words){
            TrieNode curt = root;
            for(char c : s.toCharArray()){
                int index = c - 'a';
                if(curt.subNodes[index] == null){
                    curt.subNodes[index] = new TrieNode();
                }
                curt = curt.subNodes[index];
            }
            curt.isEnd = true;
            curt.s = s;
        }
        return root;
    }
    
    class TrieNode{
        TrieNode[] subNodes = new TrieNode[26];
        String s;
        boolean isEnd = false;
        public TrieNode(){}
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: