212. Word Search II
来源:互联网 发布:java如何实现注册界面 编辑:程序博客网 时间:2024/05/06 19:06
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"]
.Note:
You may assume that all inputs are consist of lowercase letters a-z
.
Solution:
Using Dfs can generate all the possible words that the board could have, but it takes to much time and has unnecessary computation. for example if we have search "oaa" in the board above, we could stop search since no word in the words have prefix "oaa".
How do we decide if there is no word in the words array that have the prefix "xxxx" ? We build a trie.
Code:
public class Solution { int[] dirX = {1,-1,0,0}; int[] dirY = {0,0,1,-1}; public List<String> findWords(char[][] board, String[] words) { if(board.length == 0 || board[0].length == 0) return new ArrayList<String>(); TrieNode root = buildTrie(words); HashSet<String> ret = new HashSet<>(); boolean[][] visited = new boolean[board.length][board[0].length]; for(int i = 0; i < board.length; i++){ for(int j = 0; j < board[0].length; j++){ dfs(board,visited, ret, i, j, root); } } return new ArrayList<String>(ret); } public void dfs(char[][] board, boolean[][] visited, HashSet<String> ret, int i, int j, TrieNode t){ if(i < 0 || j < 0 || i >= board.length || j >= board[0].length || visited[i][j] || t.subNodes[board[i][j] - 'a'] == null){ return; } t = t.subNodes[board[i][j] - 'a']; if(t.isEnd){ ret.add(t.s); } visited[i][j] = true; for(int k = 0; k < 4; k++){ int x = i + dirX[k]; int y = j + dirY[k]; dfs(board,visited,ret,x,y,t); } visited[i][j] = false; } public TrieNode buildTrie(String[] words){ TrieNode root = new TrieNode(); for(String s : words){ TrieNode curt = root; for(char c : s.toCharArray()){ int index = c - 'a'; if(curt.subNodes[index] == null){ curt.subNodes[index] = new TrieNode(); } curt = curt.subNodes[index]; } curt.isEnd = true; curt.s = s; } return root; } class TrieNode{ TrieNode[] subNodes = new TrieNode[26]; String s; boolean isEnd = false; public TrieNode(){} }}
- 212. Word Search II
- 212. Word Search II
- 212. Word Search II
- 212. Word Search II
- 212. Word Search II
- 212. Word Search II
- 212. Word Search II
- 212. Word Search II
- 212. Word Search II
- 212. Word Search II
- [leetcode] 212.Word Search II
- LeetCode 212. Word Search II
- leetcode.212. Word Search II
- [leetcode] 212. Word Search II
- Leetcode 212. Word Search II
- 212. Word Search II(Trie)
- leetcode 212. Word Search II
- 212. Word Search II--python
- SpringBoot中文文档
- PAT-A1020
- supervisor 在python3下的简易解决方案
- 操作系统(十二)-----死锁的避免
- 数据结构-选择排序以及对它的优化
- 212. Word Search II
- PAT-A1021
- 关于如何使用按钮(onclick)打开链接
- 算法笔记_057-蓝桥杯练习 最大的算式 (Java)
- C++中assert断言函数
- android 6.0权限详解
- hadoop submit python 脚本
- 啤酒和饮料,蓝桥杯2014年第1题
- PAT-A1022