Word Search II Leetcode Java__java

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 are the those or Vertically neighboring. The same letter cell May 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"].

The key to this problem is that, using trie tree, you need to construct trie tree

All the given strings are then inserted into the trie and then traversed board

public class Solution {public list<string> findwords (char[][] board, string[] words) {Trie trie=new Tr
        IE ();
        for (int i=0;i<words.length;i++) {Trie.insert (words[i]);
        } list<string> rst=new arraylist<string> ();
        Set<string> rstset=new hashset<string> (); for (int i=0;i<board.length;i++) {for (int j=0;j<board[0].length;j++) {Checkcontain (trie,
            Board, I, J, New Boolean[board.length][board[0].length], "", Rstset);
        } for (string string:rstset) {Rst.add (string);
    return rst; 
        private void Checkcontain (Trie trie,char[][] board, int i,int j,boolean[][] visited,string s,set<string> rst) {
        S=s+string.valueof (Board[i][j]); if (visited[i][j]| |!
        Trie.startswith (s)) return;
        if (Trie.search (s)) {Rst.add (s);
        } visited[i][j]=true; if (i>0) Checkcontain (trie, board, I-1, J, visited, S, rst);
        if (i<board.length-1) Checkcontain (trie, board, I+1, J, visited, S, rst);
        if (j>0) Checkcontain (trie, board, I, J-1, visited, S, rst);
        if (j<board[0].length-1) Checkcontain (trie, board, I, J+1, visited, S, rst);
    Visited[i][j]=false;
        Class Trienode {//Initialize your data structure here.
        Public trienode[] Children=new trienode[26];;
        public char C;
        Boolean end=false;
        Public Trienode () {} public Trienode (char c) {this.c=c;

        } class Trie {private Trienode root;
        Public Trie () {root = new Trienode ();
        }//Inserts a word into the trie.
            public void Insert (String word) {Trienode cur=root;
         for (int i=0;i<word.length (); i++) {int Index=word.charat (i)-' a ';       if (cur.children[index]==null) {cur.children[index]=new Trienode (Word.charat (i));
            } Cur=cur.children[index];
        } cur.end=true;
        }//Returns If the word is in the trie.
            public boolean search (String word) {Trienode cur=root;
                for (int i=0;i<word.length (); i++) {int Index=word.charat (i)-' a ';
                if (Cur.children[index]==null) {return false;
            } Cur=cur.children[index];
        return cur.end;
        }//Returns If there is no word in the trie//that starts with the given prefix.
            public boolean startswith (String prefix) {trienode cur=root;
                for (int i=0;i<prefix.length (); i++) {int Index=prefix.charat (i)-' a ';
           if (Cur.children[index]==null) {return false;     } Cur=cur.children[index];  
        return true; }
    }
}

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.