212. 单词搜索 II

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

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/word-search-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

class Solution {

    private int[] dx = {0, 1, 0, -1};

    private int[] dy = {1, 0, -1, 0};

    private List<String> ans = new ArrayList<>();

    private char[][] board;

    private boolean[][] visited;

    private int solve(int x, int y, Trie.Node root) {
        int p = board[x][y] - 'a';

        Trie.Node cur = root.children[p];

        if (cur == null || cur.pass == 0) {
            return 0;
        }

        visited[x][y] = true;

        int sum = 0;

        if (cur.end > 0) {
            ans.add(cur.word);
            sum++;
            cur.end--;
        }

        for (int i = 0; i < 4; ++i) {
            int nx = dx[i] + x;
            int ny = dy[i] + y;
            if (nx >= 0 && nx < board.length && ny >= 0 && ny < board[0].length && !visited[nx][ny]) {
                sum += solve(nx, ny, cur);
            }
        }

        cur.pass -= sum;

        visited[x][y] = false;

        return sum;
    }

    public List<String> findWords(char[][] board, String[] words) {
        if (board == null || board.length == 0 || words == null || words.length == 0) {
            return Collections.emptyList();
        }
        this.board = board;
        Trie trie = new Trie();
        trie.insert(words);
        for (int i = 0; i < board.length; ++i) {
            for (int j = 0; j < board[0].length; ++j) {
                this.visited = new boolean[board.length][board[0].length];
                solve(i, j, trie.getRoot());
            }
        }
        return ans;
    }
}

class Trie {

    private Node root = new Node();

    class Node {
        Node[] children = new Node[26];
        int pass = 0;
        int end = 0;
        String word;
    }

    public Node getRoot() {
        return root;
    }

    public void insert(String[] words) {
        for (String word : words) {
            insert(word);
        }
    }

    public void insert(String str) {
        Node cur = root;
        cur.pass++;
        for (int i = 0; i < str.length(); ++i) {
            int path = str.charAt(i) - 'a';
            if (cur.children[path] == null) {
                cur.children[path] = new Node();
            }
            cur = cur.children[path];
            cur.pass++;
        }
        cur.end++;
        cur.word = str;
    }
}
posted @ 2022-01-05 22:14  Tianyiya  阅读(18)  评论(0)    收藏  举报