Add and Search Word - Data structure design 解答
2015-10-13 10:20
357 查看
Question
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-zor
.. 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
This kind of problem (searching a String) can be solved by Trie.We use DFS to implement search. Notice corner cases.
When we choose to implement it by recursion, think one step each time.
class TrieNode { public char value; public boolean isLeaf; public HashMap<Character, TrieNode> children; public TrieNode(char c) { value = c; children = new HashMap<Character, TrieNode>(); isLeaf = false; } } public class WordDictionary { public TrieNode root; public WordDictionary() { root = new TrieNode('!'); } // Adds a word into the data structure. public void addWord(String word) { TrieNode currentNode = root; for (int i = 0; i < word.length(); i++) { char tmp = word.charAt(i); HashMap<Character, TrieNode> children = currentNode.children; TrieNode nextNode; if (children.containsKey(tmp)) { nextNode = children.get(tmp); } else { nextNode = new TrieNode(tmp); children.put(tmp, nextNode); } currentNode = nextNode; // Check whether it's the last character if (i == word.length() - 1) currentNode.isLeaf = 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 dfsSearch(word, 0, root); } private boolean dfsSearch(String word, int index, TrieNode prevNode) { // If prevNode is null but word has not been completely traversed, return fasle if (prevNode == null) { return false; } // If word has been completely traversed, check whether tree is at bottom if (index == word.length()) { return prevNode.isLeaf; } char target = word.charAt(index); HashMap<Character, TrieNode> currentMap = prevNode.children; if (target != '.') { if (!currentMap.containsKey(target)) return false; else return dfsSearch(word, index + 1, currentMap.get(target)); } else { boolean result = false; for (Character key : currentMap.keySet()) { if (dfsSearch(word, index + 1, currentMap.get(key))) { result = true; } } return result; } } } // Your WordDictionary object will be instantiated and called as such: // WordDictionary wordDictionary = new WordDictionary(); // wordDictionary.addWord("word"); // wordDictionary.search("pattern");
相关文章推荐
- 性能测试之Nginx性能调优课程
- 神经网络入门1
- 获取文本宽度和高度
- 服务协议
- 查看 MySQL 数据库中每个表占用的空间大小
- 放松时刻——C#分割字符串
- 复制 实现两台服务器的数据同步
- Android 屏幕适配
- C++中引用传递与指针传递区别
- How to Build ffmpeg with NDK r9
- Java并发的四种风味:Thread、Executor、ForkJoin和Actor
- JavaScript执行效率小结
- 使用Docker解决同一服务器运行不同版本PHP方案。
- 10月10日全球域名注册商(国际域名)保有量及市场份额
- Win10设置在哪里?快速打开Win10设置面板快捷键介绍
- C#将指定picturebox图片保存到本地
- 前端进阶之路:点击事件绑定
- android drawable实践 xml 实现各种效果
- ios学习路线
- javascript 闭包详解