zoukankan      html  css  js  c++  java
  • [LC] 212. Word Search II

    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.

    Example:

    Input: 
    board = [
      ['o','a','a','n'],
      ['e','t','a','e'],
      ['i','h','k','r'],
      ['i','f','l','v']
    ]
    words = ["oath","pea","eat","rain"]
    
    Output: ["eat","oath"]
    

    Note:

    1. All inputs are consist of lowercase letters a-z.
    2. The values of words are distinct.

    Time: O(M * N *4^wordLen)

    class Solution {
        int gRow;
        int gCol;
        public List<String> findWords(char[][] board, String[] words) {
            TrieNode root = build(words);
            gRow = board.length;
            gCol = board[0].length;
            List<String> res = new ArrayList<>();
            for (int i = 0; i < gRow; i++) {
                for (int j = 0; j < gCol; j++) {
                    // dfs(i, j, board, root, res);
                    dfs(board, i, j, root, res);
                }
            }
            return res;
        }
        
        private void dfs(char[][] grid, int row, int col, TrieNode cur, List<String> res) {
            if (row < 0 || row >= gRow || col < 0 || col >= gCol) {
                return;
            }
            char c = grid[row][col];
            if (c == '#' || cur.children[c - 'a'] == null) {
                return;
            }
            cur = cur.children[c - 'a'];
            if (cur.word != null) {
                res.add(cur.word);
                cur.word = null;
            }
            grid[row][col] = '#';
            dfs(grid, row + 1, col, cur, res);
            dfs(grid, row - 1, col, cur, res);
            dfs(grid, row, col + 1, cur, res);
            dfs(grid, row, col - 1, cur, res);
            grid[row][col] = c;
        }
        
        private TrieNode build(String[] words) {
            TrieNode root = new TrieNode();
            for (String word : words) {
                TrieNode cur = root;
                char[] charArr = word.toCharArray();
                for (char c : charArr) {
                    if (cur.children[c - 'a'] == null) {
                        cur.children[c - 'a'] = new TrieNode();
                    }
                    cur = cur.children[c - 'a'];
                }
                cur.word = word;
            }
            return root;
        }
    }
    
    class TrieNode {
        TrieNode[] children;
        String word;
        public TrieNode() {
            children = new TrieNode[26];
            word = null;
        }
    }
  • 相关阅读:
    [转载]微信企业号开发如何建立连接
    【VB技巧】VB静态调用与动态调用dll详解
    DevExpress 中 汉化包 汉化方法
    DevExpress汉化(WinForm)
    DevExpress 中 WaitForm 使用
    DevExpress 中 DateEdit 控件 格式化显示和编辑的日期格式为: yyyy-MM-dd
    DevExpress 中 用 LookUpEdit 控件 代替 ComboBoxEdit 控件来绑定 DataTable
    asp.net 大文上传配置
    DevExpress中的lookupedit的使用方法详解
    DevExpress控件使用小结
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12710903.html
Copyright © 2011-2022 走看看