2021-05-27

212.单词搜索 II

给定一个 m x n 二维字符网格 board 和一个单词(字符串)列表 words,找出所有同时在二维网格和字典中出现的单词。

单词必须按照字母顺序,通过 相邻的单元格 内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。

 

示例 1:


输入:board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
输出:["eat","oath"]
示例 2:


输入:board = [["a","b"],["c","d"]], words = ["abcb"]
输出:[]

提示:

m == board.length
n == board[i].length
1 <= m, n <= 12
board[i][j] 是一个小写英文字母
1 <= words.length <= 3 * 104
1 <= words[i].length <= 10
words[i] 由小写英文字母组成
words 中的所有字符串互不相同

 

题解1:字典树

时间复杂度:O(M(4⋅3 L−1)),其中M 是二维网格中的单元格数,L 是单词的最大长度。

空间复杂度:O(N),其中 N 是字典中的字母总数。

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/*
 * @lc app=leetcode.cn id=212 lang=java
 *
 * [212] 单词搜索 II
 */

// @lc code=start
class Solution {
    int[] dirX = { 1, 0, -1, 0 };
    int[] dirY = { 0, -1, 0, 1 };
    Set<String> res;

    public List<String> findWords(char[][] board, String[] words) {
        // 字典树
        // 时间复杂度:O(M(4*3^l-1))
        // 空间复杂度:O(n)
        Trie node = new Trie();
        for (String word : words) {
            node.insert(word);
        }
        res = new HashSet<>();
        int m = board.length, n = board[0].length;
        boolean[][] visited = new boolean[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                dfs(board, i, j, "", node, visited);
            }
        }
        return new ArrayList<>(res);
    }

    private void dfs(char[][] board, int i, int j, String word, Trie node, boolean[][] visited) {
        // recursion terminator
        if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || visited[i][j]) {
            return;
        }
        // 记录当前单词
        word += board[i][j];
        int index = board[i][j] - 'a';
        visited[i][j] = true;
        if (node.children[index] == null) {
            // 没找到,进行回溯
            visited[i][j] = false;
            return;
        }
        node = node.children[index];
        if (node.isEnd) {
            // 找到了,就不用回溯了
            res.add(word);
        }
        for (int k = 0; k < 4; k++) {
            dfs(board, i + dirX[k], j + dirY[k], word, node, visited);
        }
        // 回溯
        visited[i][j] = false;
    }

    class Trie {
        private Trie[] children;
        private boolean isEnd;

        /** Initialize your data structure here. */
        public Trie() {
            children = new Trie[26];
            isEnd = false;
        }

        /** Inserts a word into the trie. */
        public void insert(String word) {
            Trie node = this;
            for (int i = 0; i < word.length(); i++) {
                char ch = word.charAt(i);
                int index = ch - 'a';
                if (node.children[index] == null) {
                    node.children[index] = new Trie();
                }
                node = node.children[index];
            }
            node.isEnd = true;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

suncj1314

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值