Java for Leetcode 212 Word Search II

Source: Internet
Author: User

Given a 2D board and a list of words from the dictionary, find all words in the board.

Each word must is constructed from letters of sequentially adjacent cell, where "adjacent" cells is those horizontally or Vertically neighboring. The same letter cell is 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"] .

Problem Solving Ideas:

Subject to Java for Leetcode 079 word Search But with DFS, the real practice is to put Word into trie, and then iterate through each point in the trie to see if it matches the elements in the trie, the Java implementation is as follows:

public class Solution {public list<string> findwords (char[][] board, string[] words) {hashset<string> list=n EW HashSet ();          Trie Trie = new Trie ();           for (String word:words) Trie.insert (word);        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 (list           , board, visited, "", I, J, Trie);      return new ArrayList (list); } public void Dfs (set<string> list,char[][] board, boolean[][] visited, String str, int x, int y, Trie tr          IE) {if (x < 0 | | x >= board.length | | y < 0 | | y >= board[0].length) return;          if (Visited[x][y]) return;          str + = Board[x][y];          if (!trie.startswith (str)) return;        if (Trie.search (str)) List.add (str);          Visited[x][y] = true; DFS (List,board, visited, str, x-1, y, trie);          DFS (List,board, visited, str, x + 1, y, trie);          DFS (List,board, visited, str, x, y-1, trie);          DFS (List,board, visited, str, x, y + 1, trie);      Visited[x][y] = false; }}class Trienode {//Initialize your data structure Here.int num;//How many words pass through this node, that is, the number of occurrences of the node character trienode[] son;//all son nodes Boole An isend;//is not the last node of the Char val;//node value Trienode () {this.num = 1;this.son = new Trienode[26];this.isend = false;}} Class Trie {protected Trienode root;public Trie () {root = new Trienode ();} public void Insert (String word) {if (Word = = NULL | | word.length () = = 0) return; Trienode node = this.root;char[] Letters = Word.tochararray (); for (int i = 0; i < word.length (); i++) {Int. POS = Letter S[i]-' a ', if (node.son[pos] = = null) {Node.son[pos] = new Trienode (); node.son[pos].val = Letters[i];} else {Node.son[pos] . num++;} node = Node.son[pos];} Node.isend = true;} Returns If the word is in the Trie.public boolean search (String word) {if (Word = = NULL | | word.length () = = 0) {return false;} Trienode node = root;char[] Letters = Word.tochararray (); for (int i = 0; i < word.length (); i++) {int pos = letters[i] -' a '; if (node.son[pos]! = null) {node = Node.son[pos];} else {return false;}} return node.isend;} Returns If there is any word in the trie//this starts with the given Prefix.public boolean startsWith (String prefix) { if (prefix = = NULL | | prefix.length () = = 0) {return false;} Trienode node = root;char[] Letters = Prefix.tochararray (); for (int i = 0; i < prefix.length (); i++) {int pos = letters [i]-' a '; if (node.son[pos]! = null) {node = Node.son[pos];} else {return false;}} return true;}}

Java for Leetcode 212 Word Search II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.