LeetCode_前缀树_中等_211.添加与搜索单词 - 数据结构设计

旧城等待, 2023-10-12 13:51 154阅读 0赞

目录

  • 1.题目
  • 2.思路
  • 3.代码实现(Java)

1.题目

请你设计一个数据结构,支持添加新单词和查找字符串是否与任何先前添加的字符串匹配。实现词典类 WordDictionary :

  • WordDictionary() 初始化词典对象
  • void addWord(word) 将 word 添加到数据结构中,之后可以对它进行匹配
  • bool search(word) 如果数据结构中存在字符串与 word 匹配,则返回 true ;否则,返回 false 。word 中可能包含一些 ‘.’ ,每个 . 都可以表示任何一个字母。

示例:

  1. 输入:
  2. ["WordDictionary","addWord","addWord","addWord","search","search","search","search"]
  3. [[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
  4. 输出:
  5. [null,null,null,null,false,true,true,true]
  6. 解释:
  7. WordDictionary wordDictionary = new WordDictionary();
  8. wordDictionary.addWord("bad");
  9. wordDictionary.addWord("dad");
  10. wordDictionary.addWord("mad");
  11. wordDictionary.search("pad"); // 返回 False
  12. wordDictionary.search("bad"); // 返回 True
  13. wordDictionary.search(".ad"); // 返回 True
  14. wordDictionary.search("b.."); // 返回 True

提示:
1 <= word.length <= 25
addWord 中的 word 由小写英文字母组成
search 中的 word 由 ‘.’ 或小写英文字母组成
最多调用 104 次 addWord 和 search

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/design-add-and-search-words-data-structure

2.思路

(1)前缀树
本题是对前缀树的应用,有关前缀树的具体实现可以参考【数据结构】前缀树/字典树这篇文章。

3.代码实现(Java)

  1. //思路1————前缀树
  2. class WordDictionary {
  3. private Trie root;
  4. public WordDictionary() {
  5. root = new Trie();
  6. }
  7. public void addWord(String word) {
  8. root.insert(word);
  9. }
  10. public boolean search(String word) {
  11. return dfs(word, 0, root);
  12. }
  13. private boolean dfs(String word, int index, Trie node) {
  14. if (index == word.length()) {
  15. return node.isEnd();
  16. }
  17. char c = word.charAt(index);
  18. if (Character.isLetter(c)) {
  19. //当前字符是字母
  20. int childrenIndex = c - 'a';
  21. Trie child = node.getChildren()[childrenIndex];
  22. if (child != null && dfs(word, index + 1, child)) {
  23. return true;
  24. }
  25. } else {
  26. //当前字符是 '.',它可以表示任何字母,因此对当前节点的所有非空子节点继续搜索下一个字符
  27. for (int i = 0; i < 26; i++) {
  28. Trie child = node.getChildren()[i];
  29. if (child != null && dfs(word, index + 1, child)) {
  30. return true;
  31. }
  32. }
  33. }
  34. return false;
  35. }
  36. }
  37. class Trie {
  38. private Trie[] children;
  39. private boolean isEnd;
  40. public Trie() {
  41. //每个节点最多有 26 个子节点,分别对应 26 个小写英文字母
  42. children = new Trie[26];
  43. isEnd = false;
  44. }
  45. //插入字符串
  46. public void insert(String word) {
  47. Trie node = this;
  48. for (int i = 0; i < word.length(); i++) {
  49. int index = word.charAt(i) - 'a';
  50. if (node.children[index] == null) {
  51. node.children[index] = new Trie();
  52. }
  53. node = node.children[index];
  54. }
  55. node.isEnd = true;
  56. }
  57. public Trie[] getChildren() {
  58. return children;
  59. }
  60. public boolean isEnd() {
  61. return isEnd;
  62. }
  63. }
  64. /**
  65. * Your WordDictionary object will be instantiated and called as such:
  66. * WordDictionary obj = new WordDictionary();
  67. * obj.addWord(word);
  68. * boolean param_2 = obj.search(word);
  69. */

发表评论

表情:
评论列表 (有 0 条评论,154人围观)

还没有评论,来说两句吧...

相关阅读