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;  
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值