package design; import java.util.HashMap; import java.util.Map; /** * Created by gouthamvidyapradhan on 09/12/2017. * *
Design a data structure that supports the following two 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") -> false search("bad") -> true * search(".ad") -> true search("b..") -> true Note: You may assume that all words are consist of * lowercase letters a-z. * *
Solution: Implement a simple Trie and perform a search.
*/
public class WordDictionary {
private Trie trie;
/**
* Main method
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
WordDictionary wd = new WordDictionary();
wd.addWord("bad");
wd.addWord("dad");
wd.addWord("mad");
System.out.println(wd.search("pad"));
System.out.println(wd.search("bad"));
System.out.println(wd.search(".ad"));
System.out.println(wd.search("..."));
}
/** Initialize your data structure here. */
public WordDictionary() {
this.trie = new Trie();
}
/** Adds a word into the data structure. */
public void addWord(String word) {
this.trie.insert(word);
}
/**
* Returns if the word is in the data structure. A word could contain the dot character '.' to
* represent any one letter.
*/
public boolean search(String word) {
return this.trie.search(word);
}
private class Trie {
private Map