211. Add and Search Word
Problem:
这次是自己想出来的如何处理‘.’。有.时候,需要遍历当前trienode所有的children。所以用递归比较好实现。
TrieNode的children用hashmap更方便。而且居然自己想出来如何处理,for循环里面boolean结果的方法,感觉快出山了。
--------------------------------------------------------------------------------------------------------------------------
3/6/2018 update
Analysis:
This problem is very important as it applies DFS to new field. Not the typical get a set of results DFS. But it still needs to follow the DFS template.
If DFS result is boolean, and there is a for loop to go to child nodes. We can follow the below template
Solution:
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..") -> true6/5/2018 update:
这次是自己想出来的如何处理‘.’。有.时候,需要遍历当前trienode所有的children。所以用递归比较好实现。
TrieNode的children用hashmap更方便。而且居然自己想出来如何处理,for循环里面boolean结果的方法,感觉快出山了。
class WordDictionary { class TrieNode { Map<Character, TrieNode> children; boolean isWord; public TrieNode() { children = new HashMap<>(); } } TrieNode root; /** Initialize your data structure here. */ public WordDictionary() { root = new TrieNode(); } /** Adds a word into the data structure. */ public void addWord(String word) { TrieNode cur = root; for (char c: word.toCharArray()) { if (!cur.children.containsKey(c)) { cur.children.put(c, new TrieNode()); } cur = cur.children.get(c); } cur.isWord = true; } /** 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 helper(word, 0, root); } private boolean helper(String word, int index, TrieNode cur) { if (index == word.length()) { return cur.isWord; } char c = word.charAt(index); if (c != '.') { return cur.children.containsKey(c) && helper(word, index + 1, cur.children.get(c)); } else { for (TrieNode node: cur.children.values()) { if (helper(word, index + 1, node)) { return true; } } return false; } } } /** * Your WordDictionary object will be instantiated and called as such: * WordDictionary obj = new WordDictionary(); * obj.addWord(word); * boolean param_2 = obj.search(word); */
--------------------------------------------------------------------------------------------------------------------------
3/6/2018 update
Analysis:
This problem is very important as it applies DFS to new field. Not the typical get a set of results DFS. But it still needs to follow the DFS template.
If DFS result is boolean, and there is a for loop to go to child nodes. We can follow the below template
for (TrieNode n: node.children) { if( n != null && helper(word, n, index + 1)) return true; } return false;
Solution:
class WordDictionary { class TrieNode { char val; TrieNode[] children; boolean isWord; public TrieNode() {} public TrieNode(char val) { this.val = val; children = new TrieNode[26]; } } TrieNode root; /** Initialize your data structure here. */ public WordDictionary() { root = new TrieNode(' '); } /** Adds a word into the data structure. */ public void addWord(String word) { TrieNode node = root; for (char c: word.toCharArray()) { if (node.children[c - 'a'] == null) { node.children[c - 'a'] = new TrieNode(c); } node = node.children[c - 'a']; } node.isWord = true; } /** 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 helper(word, root, 0); } private boolean helper(String word, TrieNode node, int index) { if (index == word.length()) return node.isWord; char c = word.charAt(index); if (c == '.') { for (TrieNode n: node.children) { if( n != null && helper(word, n, index + 1)) return true; } return false; } else { return node.children[c - 'a'] != null && helper(word, node.children[c - 'a'], index + 1); } } } /** * Your WordDictionary object will be instantiated and called as such: * WordDictionary obj = new WordDictionary(); * obj.addWord(word); * boolean param_2 = obj.search(word); */
评论
发表评论