Word Search II

来源:互联网 发布:js闭包循环引用 编辑:程序博客网 时间:2024/04/30 06:34

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.

思路:这题直接暴力用backtracking会超时,hint提示用了trie。思路就是对于矩阵里面的每一个点为起点开始搜索,如果startwith trie,那么就开始搜索,search到了就加到hashset里面,最后返回。因为trie的搜索很快,所以不会超时。

public class Solution {     public List<String> findWords(char[][] board, String[] words) {         List<String> list = new ArrayList<String>();         if(board == null || board.length == 0 || board[0].length == 0 || words == null || words.length == 0) {             return list;         }                  Trie tries = new Trie();         for(String str: words){             tries.insert(str);         }                  HashSet<String> hashset = new HashSet<String>();         boolean[][] visited = new boolean[board.length][board[0].length];         StringBuilder sb = new StringBuilder();         for(int i=0; i<board.length; i++){             for(int j=0; j<board[0].length; j++){                 collect(board, i, j, tries, visited,sb, hashset);             }         }         return new ArrayList<String>(hashset);     }          public void collect(char[][] board, int i, int j, Trie tries, boolean[][] visited, StringBuilder sb, HashSet<String> hashset){         if(i<0 || i>=board.length || j<0 || j>=board[0].length || visited[i][j] ){             return ;         }                  sb.append(board[i][j]);         visited[i][j] = true;         String str = sb.toString();         if(tries.startwith(str)){             if(tries.search(str)){                 hashset.add(str);             }             collect(board, i+1, j, tries, visited, sb, hashset);             collect(board, i-1, j, tries, visited, sb, hashset);             collect(board, i, j+1, tries, visited, sb, hashset);             collect(board, i, j-1, tries, visited, sb, hashset);         }                 visited[i][j] = false;         sb.deleteCharAt(sb.length()-1);     }          public class Trie{         public class TrieNode{             TrieNode[] array;             boolean isword;                          public TrieNode() {                 array = new TrieNode[26];                 isword = false;             }         }                  private TrieNode root;         public Trie() {             root = new TrieNode();         }                  public void insert(String word){             if(word == null || word.length() == 0) return;             TrieNode curNode = root;             for(int i=0; i<word.length(); i++){                 char c = word.charAt(i);                 if(curNode.array[c-'a'] == null) {                     TrieNode temp = new TrieNode();                     curNode.array[c-'a'] = temp;                     curNode = temp;                 } else {                     curNode = curNode.array[c-'a'];                 }             }             curNode.isword = true;         }                  public TrieNode searchNode(String word){             TrieNode curNode = root;             for(int i=0; i<word.length(); i++){                 char c = word.charAt(i);                 if(curNode.array[c-'a'] == null) {                     return null;                 } else {                     curNode = curNode.array[c-'a'];                 }             }             return curNode == root ? null : curNode;         }                  public boolean search(String word){             TrieNode node = searchNode(word);             return node!= null && node.isword;         }                  public boolean startwith(String prefix){             TrieNode node = searchNode(prefix);             return node!=null;         }              } }


0 0
原创粉丝点击