Description:
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 .
The topic is well understood: add words to find words.
The first thing to think about is to use linear data structures, and then match each lookup. It can be imagined that it must be timed out .
1 Public classWorddictionary {2 3 Privatelist<string> arr =NewArraylist<string>();4 5 //Adds A word into the data structure.6 Public voidAddword (String word) {7 Arr.add (word);8 }9 Ten //Returns If the word is in the data structure. A Word could One //contain the dot character '. ' to represent. A Public BooleanSearch (String word) { - BooleanFlag =true; - for(String str:arr) { the if(Str.equals (Word)) { -Flag =true; - Break; - } + Else if(Arr.contains (".")) { - if(Word.length ()! =str.length ()) { +Flag =false; A Break; at } - - for(inti=0; i<str.length ();) { - CharCH1 =Str.charat (i); - CharCH2 =Word.charat (i); - if(Ch1 = = '. ' | | ch2 = = '. ' | | ch1 = =CH2) { ini + +; - } to Else { +Flag =false; - Break; the } * } $ Panax Notoginseng } - Else { theFlag =false; + Break; A } the } + returnFlag; - } $ } $ - //Your Worddictionary object would be instantiated and called as such: - //worddictionary worddictionary = new Worddictionary (); the //Worddictionary.addword ("word"); - //Worddictionary.search ("pattern");
To optimize the obvious use of trie trees to reduce the number of useless comparisons, thus reducing the complexity of time.
Public classWorddictionary {PrivateTrienode Root; Publicworddictionary () { This. root =NewTrienode (); } Private classTrienode {Privatetrienode[] Son; Private CharVal; Private BooleanIsend; PublicTrienode () { This. son =NewTrienode[26]; This. Isend =false; } } //Adds A word into the data structure. Public voidAddword (String word) {Char[] Wordchars =Word.tochararray (); Trienode node= This. Root; for(Charch:wordchars) { intpos = CH-' A '; if(Node.son[pos] = =NULL) {Node.son[pos]=NewTrienode (); Node.son[pos].val=ch; } node=Node.son[pos]; } node.isend=true; } Public BooleanPatternsearch (String Word, trienode node) {Char[] Wordchars =Word.tochararray (); for(intat=0; At<word.length (); at++) { CharCH =Wordchars[at]; if(ch! = '. ')) { intpos = CH-' A '; if(Node.son[pos]! =NULL) {node=Node.son[pos]; } Else { return false; } } Else { intFlag = 0; for(inti=0; i<26; i++) { if(Node.son[i]! =NULL) { Booleanb =patternsearch (word.substring (at+1), Node.son[i]); if(b)returnb; Else{flag++; } } Else{flag++; Continue; } } if(Flag = = 26) { return false; } } } returnNode.isend; } //Returns If the word is in the data structure. A Word could//contain the dot character '. ' to represent. Public BooleanSearch (String word) {returnPatternsearch (Word, This. Root); }}
Leetcode--add and Search word-data structure design