7 Star 32 Fork 21

空無一悟 / algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
WordDictionary.java 1.46 KB
一键复制 编辑 原始数据 按行查看 历史
空無一悟 提交于 2021-09-22 23:47 . init
/***********************************************************
* @Description : 字典查询,LeetCode题目211
* https://leetcode-cn.com/problems/add-and-search-word-data-structure-design/
* @author : 梁山广(Laing Shan Guang)
* @date : 2020/1/2 10:41
* @email : liangshanguang2@gmail.com
***********************************************************/
package Chapter10Trie.Section5Leetcode211TrieAndPatternMatch;
public class WordDictionary {
private TrieRegex trieRegex;
/** Initialize your data structure here. */
public WordDictionary() {
trieRegex = new TrieRegex();
}
/** Adds a word into the data structure. */
public void addWord(String word) {
trieRegex.add(word);
}
/** 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 trieRegex.contains(word);
}
public static void main(String[] args) {
WordDictionary dictionary = new WordDictionary();
dictionary.addWord("bad");
dictionary.addWord("dad");
dictionary.addWord("mad");
dictionary.addWord("world");
// 测试单词查找
System.out.println(dictionary.search("pad")); // false
System.out.println(dictionary.search("bad")); // true
System.out.println(dictionary.search(".ad")); // true
System.out.println(dictionary.search("b..")); // true
}
}
1
https://gitee.com/lsgwr/algorithms.git
git@gitee.com:lsgwr/algorithms.git
lsgwr
algorithms
algorithms
master

搜索帮助