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.
publicclassWordDictionary{ TrieNode root = new TrieNode(); privateclassTrieNode{ publicboolean isWord; public TrieNode[] children; publicTrieNode(){ isWord = false; children = new TrieNode[26]; } }
// Adds a word into the data structure. publicvoidaddWord(String word){ if (word != null){ 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(); } cur = cur.children[index]; } cur.isWord = true; } }
// Returns if the word is in the data structure. A word could // contain the dot character '.' to represent any one letter. publicbooleansearch(String word){ if (word == null) returnfalse; return searchArray(word.toCharArray(), 0, root); } publicbooleansearchArray(char[] chars, int start, TrieNode node){ TrieNode cur = node; for (int i = start; i < chars.length; i++){ if (cur == null){ break; }else{ if (chars[i] != '.'){ int index = chars[i] - 'a'; cur = cur.children[index]; }else{ TrieNode temp = cur; for (int j = 0; j < 26; j++){ cur = temp.children[j]; if (searchArray(chars, i+1, cur)) returntrue; } returnfalse; } } } return cur != null && cur.isWord; } }
// Your WordDictionary object will be instantiated and called as such: // WordDictionary wordDictionary = new WordDictionary(); // wordDictionary.addWord("word"); // wordDictionary.search("pattern");
Map<Integer, List<String>> map = new HashMap<Integer, List<String>>(); // Adds a word into the data structure. publicvoidaddWord(String word){ int index = word.length(); if(!map.containsKey(index)){ List<String> list = new ArrayList<String>(); list.add(word); map.put(index, list); }else{ map.get(index).add(word); }
}
// Returns if the word is in the data structure. A word could // contain the dot character '.' to represent any one letter. publicbooleansearch(String word){ int index = word.length(); if(!map.containsKey(index)){ returnfalse; } List<String> list = map.get(index); if(isWords(word)){ return list.contains(word); } for(String s : list){ if(isSame(s, word)){ returntrue; } } returnfalse; }
booleanisWords(String s){ for(int i = 0; i < s.length(); i++){ if(!Character.isLetter(s.charAt(i))){ returnfalse; } } returntrue; }
booleanisSame(String a, String search){ if(a.length() != search.length()){ returnfalse; } for(int i = 0; i < a.length(); i++){ if(search.charAt(i) != '.' && search.charAt(i) != a.charAt(i)){ returnfalse; } } returntrue; } }
// Your WordDictionary object will be instantiated and called as such: // WordDictionary wordDictionary = new WordDictionary(); // wordDictionary.addWord("word"); // wordDictionary.search("pattern");