212. Word Search II

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"].

一刷
題解:
因?yàn)闉榱艘焖倥袛嘤胐fs構(gòu)成的prefix是否存在,這里用trie來(lái)保存字典。

public class Solution {
    class TrieNode {
        TrieNode[] next = new TrieNode[26];
        String word;
    }
    
    public List<String> findWords(char[][] board, String[] words) {
        List<String> res = new ArrayList<>();
        TrieNode root = buildTrie(words);
        for(int i=0; i<board.length; i++){
            for(int j=0; j<board[0].length; j++){
                dfs(board, i, j, root, res);
            }
        }
        return res;
    }
    
    private void dfs(char[][] board, int i, int j, TrieNode node, List<String> res){
        if(i<0 || j<0 || i>=board.length || j>=board[0].length) return;
        char c = board[i][j];
        if(c == '#' || node.next[c - 'a'] == null) return;
        node = node.next[c-'a'];
        if(node.word!=null){//found
            res.add(node.word);
            node.word = null;
        }
        board[i][j] = '#';
        dfs(board, i-1, j, node, res);
        dfs(board, i+1, j, node, res);
        dfs(board, i, j-1, node, res);
        dfs(board, i, j+1, node, res);
        board[i][j] = c;
    }
    
    
    private TrieNode buildTrie(String[] words){
        TrieNode root = new TrieNode();
        for(String word : words){
            TrieNode node = root;
            for(char ch : word.toCharArray()){
                if(node.next[ch - 'a'] == null){
                    node.next[ch - 'a'] = new TrieNode();
                }
                node = node.next[ch - 'a'];
            }
            node.word = word;
        }
        return root;
    }
}

二刷
這里有兩個(gè)小細(xì)節(jié)值得注意,首先TrieNode出了有26個(gè)child, 還有一個(gè)String field, 可以遍于最后加入list中。
第二,如果遍歷到了這個(gè)單詞,將它置為null, 否則會(huì)重復(fù),或者用set保存最終結(jié)果。

public class Solution {
    private class TrieNode{
        TrieNode[] child = new TrieNode[26];
        String word;
    }
    
    private TrieNode root = new TrieNode();
    
    public List<String> findWords(char[][] board, String[] words) {
        //buildTree
        List<String> res = new ArrayList<>();
        buildTree(words);
        for(int i=0; i<board.length; i++){
            for(int j=0; j<board[0].length; j++){
                dfs(board, i, j, root, res);
            }
        }
        return res;
    }
    
    
    private void dfs(char[][] board, int i, int j, TrieNode cur, List<String> res){
        if(i<0 || i>=board.length || j<0 || j>=board[0].length) return;
        char ch = board[i][j];
        if(ch == '#' || cur.child[board[i][j] - 'a'] == null) return;
        cur = cur.child[board[i][j] - 'a'];
        if(cur.word != null) {
            res.add(cur.word);
            cur.word = null;
        }
        board[i][j] = '#';
        dfs(board, i+1, j, cur, res);
        dfs(board, i-1, j, cur, res);
        dfs(board, i, j+1, cur, res);
        dfs(board, i, j-1, cur, res);
        board[i][j] = ch;
    }
    
    private void buildTree(String[] words){
        for(String word : words){
            TrieNode cur = root;
            for(int i=0; i<word.length(); i++){
                char ch = word.charAt(i);
                if(cur.child[ch - 'a'] == null){
                    cur.child[ch - 'a'] = new TrieNode();
                }
                cur = cur.child[ch - 'a'];
            }
            cur.word = word;
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容