212. Word Search II

Question

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

Code

class TrieNode {
    boolean end;
    Map<Character, TrieNode> sons;
    String s;
    
    public TrieNode() {
        sons = new HashMap<>();
    }
}

class Trie {
    public TrieNode root;
    
    public Trie() {
        root = new TrieNode();
    }
    
    public void insert (String s) {
        if (s == null || s.length() == 0) return;
        TrieNode node = root;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!node.sons.containsKey(c)) {
                TrieNode newNode = new TrieNode();
                node.sons.put(c, newNode);
            }
            node = node.sons.get(c);
        }
        node.end = true;
        node.s = s;
    }
    
    public boolean search(String s) {
        if (s == null || s.length() == 0) return true;
        
        TrieNode node = root;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (node.sons.containsKey(c)) {
                node = node.sons.get(c);
            } else {
                return false;
            }
        }
        
        return node.end;
    }
}

public class Solution {
    private int[] dx = {1, 0, -1, 0};
    private int[] dy = {0, 1, 0, -1};
    private Trie t;
    
    public List<String> findWords(char[][] board, String[] words) {
        List<String> result = new ArrayList<>();
        
        t = new Trie();
        for (String word: words) {
            t.insert(word);
        }
        
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                search(board, i, j, t.root, result);
            }
        }
        return result;
    }
    
    public void search(char[][] board, int x, int y, TrieNode node, List<String> result) {
        if (node.end) {
            if (!result.contains(node.s)) {
                result.add(node.s);
            }
        }
        if (x < 0 || x >= board.length || y < 0 || y >= board[0].length || board[x][y] == 0 || node == null) return;
        if (node.sons.containsKey(board[x][y])) {
            for (int i = 0; i < 4; i++) {
                char c = board[x][y];
                board[x][y] = 0;
                search(board, x + dx[i], y + dy[i], node.sons.get(c), result);
                board[x][y] = c;
            }
        }
    }
}

Solution

用字典樹實(shí)現(xiàn)。講所有字符串加進(jìn)字典樹。DFS遍歷board,將符合條件的字符串加入結(jié)果集中。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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