设计一个支持以下两种操作的数据结构:
void addWord(word) bool search(word)
search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 .
或 a-z
。 .
可以表示任何一个字母。
示例:
addWord("bad") addWord("dad") addWord("mad") search("pad") -> false search("bad") -> true search(".ad") -> true search("b..") -> true
说明:
你可以假设所有单词都是由小写字母
a-z
组成的。
class WordDictionary {
public:
/** Initialize your data structure here. */
WordDictionary() {
vector<string> Dic ;
}
vector<string> Dic ;
/** Adds a word into the data structure. */
void addWord(string word) {
Dic.push_back(word) ;
}
/** 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) {
bool flag = false ;
for(int i = 0 ;i<Dic.size();i++)
{
for(int j = 0;j< word.size();j++)
{
if(Dic[i].size() != word.size())
{
break;
}
if(word[j]=='.'||word[j]==Dic[i][j])
{
flag = true ;
}
else
{
flag = false ;
}
if(flag == false)
{
break ;
}
}
if(flag == true)
{
break ;
}
public:
/** Initialize your data structure here. */
WordDictionary() {
vector<string> Dic ;
}
vector<string> Dic ;
/** Adds a word into the data structure. */
void addWord(string word) {
Dic.push_back(word) ;
}
/** 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) {
bool flag = false ;
for(int i = 0 ;i<Dic.size();i++)
{
for(int j = 0;j< word.size();j++)
{
if(Dic[i].size() != word.size())
{
break;
}
if(word[j]=='.'||word[j]==Dic[i][j])
{
flag = true ;
}
else
{
flag = false ;
}
if(flag == false)
{
break ;
}
}
if(flag == true)
{
break ;
}
}
if(flag)
{
return true ;
}
else
{
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);
*/
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* bool param_2 = obj.search(word);
*/