[Leetcode] 211. ADD and Search word-data structure design Java

Source: Internet
Author: User

Topic:

Design a data structure that supports the following and the operations:

void Addword (Word) bool Search (word)

Search (Word) can search a literal word or a regular expression string containing only letters a-z or . . A . means it can represent any one letter.

For example:

Addword ("Bad") Addword ("Dad") Addword ("Mad") Search ("pad"), Falsesearch ("bad"), Truesearch (". Ad") Truesearch ("B..")-True

Note:
You may assume this all words is consist of lowercase letters a-z .

test instructions and Analysis: design a data structure that supports insertions and lookups, the inserted string contains only a A-Z lowercase letter, and the found string can contain '. ' (representing any one letter). This problem can actually require a dictionary tree, each tree node has 26 child nodes, there is a Boolean type to determine whether the string from the root node to that point is a letter. Query if you encounter '. ' The traversal queries all the child nodes of the current node, if there is a match can be found, otherwise cannot find.

Code:

classtrienode{ Public CharWord//Letters     Public BooleanIsword;//determines whether a complete word is from the root node to the point     Publictrienode[] Childrens =NewTRIENODE[26];//child Nodes     PublicTrienode () {} PublicTrienode (Charword0) {Trienode Trienode=NewTrienode (); Trienode.word=word0; }} Public classworddictionary {Trienode root; /**Initialize your data structure here.*/     PublicWorddictionary () {root=NewTrienode (); Root.word= ' '; }    /**Adds A word into the data structure.*/     Public voidAddword (String word) {trienode node=Root;  for(inti=0;i<word.length ();) { while(I<word.length () &&node.childrens[word.charat (i)-' a ']!=NULL) {node=node.childrens[word.charat (i)-' a ']; I++; }             while(i<word.length ()) {Node.childrens[word.charat (i)-' a '] =NewTrienode (Word.charat (i)); Node= Node.childrens[word.charat (i)-' a ']; I++; }} Node.isword=true; }    /**Returns If the word is in the data structure. A word could contain the dot character '. ' to represent all one letter. */     Public BooleanSearch (String word) {trienode node=Root; returnSearchres (0, Word,node); }    Private BooleanSearchres (intstart,string word,trienode node) {        if(Start = =word.length ()) {            if(node.isword==true)return true; Else return false; }        if(Word.charat (start) = = '. ') {//If a point is '. '            BooleanIsword =false;  for(inti=0;i<26;i++){                if(node.childrens[i]!=NULL) {Isword= isword| | Searchres (start+1, Word,node.childrens[i]); }            }            returnIsword; }Else{            if(Node.childrens[word.charat (Start)-' a ']!=NULL){                returnSearchres (Start+1,word,node.childrens[word.charat (start)-' a ']); }Else                return false; }    }}/*** Your Worddictionary object would be instantiated and called as such: * worddictionary obj = new Worddictionary (); * Obj.addword (word); * Boolean param_2 = Obj.search (word); */

[Leetcode] 211. ADD and Search word-data structure design Java

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.