Word Search II 解题报告

来源:互联网 发布:淘宝买东西寄到国外 编辑:程序博客网 时间:2024/05/17 01:35

转载自:http://blog.csdn.net/ljiabin/article/details/45846527

  【题目】

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.

【解析】

参考 【LeetCode】Word Search 解题报告 ,题目变成给定一组word,检查哪个word可以由board形成。

如果还按照DFS回溯的方法,逐个检查每个word是否在board里,显然效率是比较低的。我们可以利用Trie数据结构,也就是前缀树。然后dfs时,如果当前形成的单词不在Trie里,就没必要继续dfs下去了。如果当前字符串在trie里,就说明board可以形成这个word。

【Java代码】

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class Solution {  
  2.     Set<String> res = new HashSet<String>();  
  3.       
  4.     public List<String> findWords(char[][] board, String[] words) {  
  5.         Trie trie = new Trie();  
  6.         for (String word : words) {  
  7.             trie.insert(word);  
  8.         }  
  9.           
  10.         int m = board.length;  
  11.         int n = board[0].length;  
  12.         boolean[][] visited = new boolean[m][n];  
  13.         for (int i = 0; i < m; i++) {  
  14.             for (int j = 0; j < n; j++) {  
  15.                 dfs(board, visited, "", i, j, trie);  
  16.             }  
  17.         }  
  18.           
  19.         return new ArrayList<String>(res);  
  20.     }  
  21.       
  22.     public void dfs(char[][] board, boolean[][] visited, String str, int x, int y, Trie trie) {  
  23.         if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) return;  
  24.         if (visited[x][y]) return;  
  25.           
  26.         str += board[x][y];  
  27.         if (!trie.startsWith(str)) return;  
  28.           
  29.         if (trie.search(str)) {  
  30.             res.add(str);  
  31.         }  
  32.           
  33.         visited[x][y] = true;  
  34.         dfs(board, visited, str, x - 1, y, trie);  
  35.         dfs(board, visited, str, x + 1, y, trie);  
  36.         dfs(board, visited, str, x, y - 1, trie);  
  37.         dfs(board, visited, str, x, y + 1, trie);  
  38.         visited[x][y] = false;  
  39.     }  
  40. }  


Trie数据结构的实现在LeetCode上也有对应的题目 Implement Trie (Prefix Tree) 

【Trie实现】

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. class TrieNode {  
  2.     public TrieNode[] children = new TrieNode[26];  
  3.     public String item = "";  
  4.       
  5.     // Initialize your data structure here.  
  6.     public TrieNode() {  
  7.     }  
  8. }  
  9.   
  10. class Trie {  
  11.     private TrieNode root;  
  12.   
  13.     public Trie() {  
  14.         root = new TrieNode();  
  15.     }  
  16.   
  17.     // Inserts a word into the trie.  
  18.     public void insert(String word) {  
  19.         TrieNode node = root;  
  20.         for (char c : word.toCharArray()) {  
  21.             if (node.children[c - 'a'] == null) {  
  22.                 node.children[c - 'a'] = new TrieNode();  
  23.             }  
  24.             node = node.children[c - 'a'];  
  25.         }  
  26.         node.item = word;  
  27.     }  
  28.   
  29.     // Returns if the word is in the trie.  
  30.     public boolean search(String word) {  
  31.         TrieNode node = root;  
  32.         for (char c : word.toCharArray()) {  
  33.             if (node.children[c - 'a'] == nullreturn false;  
  34.             node = node.children[c - 'a'];  
  35.         }  
  36.         return node.item.equals(word);  
  37.     }  
  38.   
  39.     // Returns if there is any word in the trie  
  40.     // that starts with the given prefix.  
  41.     public boolean startsWith(String prefix) {  
  42.         TrieNode node = root;  
  43.         for (char c : prefix.toCharArray()) {  
  44.             if (node.children[c - 'a'] == nullreturn false;  
  45.             node = node.children[c - 'a'];  
  46.         }  
  47.         return true;  
  48.     }  
  49. }  

0 0