211. Add and Search Word
来源:互联网 发布:java替换指定字符串 编辑:程序博客网 时间:2024/05/05 16:10
- description
- implementation
description
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") -> falsesearch("bad") -> truesearch(".ad") -> truesearch("b..") -> true
From the description, it is clear that it was a problem relative to trie(prefix tree).
As the range for each node is a - z, so every parent node needs 26 children.
So this problem can be write as :
Data structure:
1 node: each node has 26 children and a flag to indicate whether the current node is termination of a node or not.
2 root: constructor a new node to root.
search(): utilize BFS when meeting with ‘.’. When the char is ‘.’, store all children which is not NULL into queue. When the char c is not ‘.’, judge the child[c-‘a’] is exists or not.
If the queue is empty before we got the right result, return false; if we get the end of string end and there is no isKey == true, return false.
implementation
class TrieNode{public: TrieNode* child[26]; bool isKey; TrieNode():isKey(false) { memset(child, NULL, 26*sizeof(TrieNode*)); }};class WordDictionary {private: TrieNode* root;public: /** Initialize your data structure here. */ WordDictionary() { this->root = new TrieNode(); } /** Adds a word into the data structure. */ void addWord(string word) { int len = word.length(); TrieNode* cur = root; for(int i = 0; i < len; i++) { if(!cur->child[word[i]-'a']) cur->child[word[i]-'a'] = new TrieNode(); cur = cur->child[word[i]-'a']; } cur->isKey = true; } /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */ bool search(string word) { int len = word.length(); TrieNode* cur = root; queue<TrieNode*> que; que.push(root); for(int i = 0; i < len; i++) { int sz = que.size(); if(!sz) return false; for(int j = 0; j < sz; j++) { cur = que.front(); que.pop(); if(word[i] == '.') { for(int k = 0; k < 26; k++) { if(cur->child[k]) { if(i == len - 1 && cur->child[k]->isKey) return true; que.push(cur->child[k]); } } } else if(cur->child[word[i]-'a']) { if(i == len - 1 && cur->child[word[i]-'a']->isKey) return true; que.push(cur->child[word[i]-'a']); } } } return false; }};/** * Your WordDictionary object will be instantiated and called as such: * WordDictionary obj = new WordDictionary(); * obj.addWord(word); * bool param_2 = obj.search(word); */
- 211. Add and Search Word
- 211. Add and Search Word
- 211. Add and Search Word
- add-and-search-word
- Add and Search Word
- Add and Search Word
- Add and Search Word
- Leetcode 211. Add and Search Word
- [Leetcode] 211. Add and Search Word
- [leetcode]211. Add and Search Word
- leetcode 211. Add and Search Word
- leetcode 211. Add and Search Word
- leetcode 211. Add and Search Word
- [LintCode]Add and Search Word
- [LeetCode] Add and Search Word
- Leetcode211. Add and Search Word
- leetcode--Add and Search Word
- LeetCode211. Add and Search Word
- Spring注解之实体类常用注解
- 从 127.0.0.1 到 127.255.255.254
- Machine Learning with Scikit-Learn and Tensorflow 7.6 Extra-Trees
- screen命令
- pscc 自动生成图像资源
- 211. Add and Search Word
- Hive 五种数据导入方式介绍
- struts2 漏洞编号S2-045 升级方案 更换jar包清单
- UGUI背包实现详解之一UGUI精准拖拽
- leedcood一刷--Binary Search
- L2-019. 悄悄关注
- 文章标题
- My97DatePicker控件的使用
- ¥ 的字体大小和价格的字体大小不一致时,¥和文字不能底部居中