Word Search II Leetcode Java

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

這道題目的關鍵在於,使用trie tree,所以需要自己構造trie tree

然後將所有給定字符串插入到trie 中,再遍歷board

public class Solution {
    public List<String> findWords(char[][] board, String[] words) {
        Trie trie=new Trie();
        for(int i=0;i<words.length;i++){
            trie.insert(words[i]);
        }
        List<String> rst=new ArrayList<String>();
        Set<String> rstSet=new HashSet<String>();
        for(int i=0;i<board.length;i++){
            for(int j=0;j<board[0].length;j++){
                checkContain(trie, board, i, j, new boolean[board.length][board[0].length], "", rstSet);
            }
        }
        for(String string:rstSet){
            rst.add(string);
        }
        return rst;
    }
    private void checkContain(Trie trie,char[][] board, int i,int j,boolean[][] visited,String s,Set<String> rst){
        s=s+String.valueOf(board[i][j]);
        if(visited[i][j]||!trie.startsWith(s))
            return;
        if(trie.search(s)){
            rst.add(s);
        }
        visited[i][j]=true;
        if(i>0)
            checkContain(trie, board, i-1, j, visited, s, rst);
        if(i<board.length-1)
            checkContain(trie, board, i+1, j, visited, s, rst);
        if(j>0)
            checkContain(trie, board, i, j-1, visited, s, rst);
        if(j<board[0].length-1)
            checkContain(trie, board, i, j+1, visited, s, rst);
        visited[i][j]=false;
    }
    class TrieNode {
        // Initialize your data structure here.
        public TrieNode[] children=new TrieNode[26];;
        public char c;
        boolean end=false;
        public TrieNode() {
        }
        public TrieNode(char c){
            this.c=c;
        }
    }

    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++){
                int index=word.charAt(i)-'a';
                if(cur.children[index]==null){
                    cur.children[index]=new TrieNode(word.charAt(i));
                }
                cur=cur.children[index];
            }
            cur.end=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++){
                int index=word.charAt(i)-'a';
                if(cur.children[index]==null){
                    return false;
                }
                cur=cur.children[index];
            }
            return cur.end;
        }

        // 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++){
                int index=prefix.charAt(i)-'a';
                if(cur.children[index]==null){
                    return false;
                }
                cur=cur.children[index];
            }
            return true;  
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章