Leetcode: Add and Search Word - Data structure design

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") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true
Note:
You may assume that all words are consist of lowercase letters a-z.

This is related to previous problem: Implement Trie

Only because in this problem, "." is introduced, which will cause us to backtrack to check each son. Using iteration is not that convient, we use recursion here.

One thing to be cautious is that when inserting words to a trie, the last node should have node.isEnd set to be true. Always forget this

 1 public class WordDictionary {
 2     public class TrieNode {
 3         int num; //count how many words go through this TrieNode
 4         TrieNode[] sons; //Arrays of children
 5         boolean isEnd;
 6         char val;
 7
 8         public TrieNode() {
 9             this.num = 0;
10             this.sons = new TrieNode[26];
11             this.isEnd = false;
12         }
13     }
14
15     TrieNode root;
16
17     public WordDictionary() {
18         this.root = new TrieNode();
19     }
20
21     // Adds a word into the data structure.
22     public void addWord(String word) {
23         if (word==null || word.length()==0) return;
24         char[] arr = word.toCharArray();
25         TrieNode node = this.root;
26         for (char c : arr) {
27             int pos = (int)(c - ‘a‘);
28             if (node.sons[pos] == null) {
29                 node.sons[pos] = new TrieNode();
30                 node.sons[pos].num = 1;
31                 node.sons[pos].val = c;
32             }
33             else {
34                 node.sons[pos].num++;
35             }
36             node = node.sons[pos];
37         }
38         node.isEnd = true;
39     }
40
41     // Returns if the word is in the data structure. A word could
42     // contain the dot character ‘.‘ to represent any one letter.
43     public boolean search(String word) {
44         int len = word.length();
45         TrieNode node = this.root;
46         return search(word, node, 0, len);
47     }
48
49     public boolean search(String word, TrieNode node, int cur, int len) {
50         if (cur == len) return node.isEnd;
51         char w = word.charAt(cur);
52         if (w == ‘.‘) {
53             for (int j=0; j<26; j++) {
54                 if (node.sons[j] != null) {
55                     if (search(word, node.sons[j], cur+1, len))
56                         return true;
57                 }
58             }
59             return false;
60         }
61         else {
62             int pos = (int)(w - ‘a‘);
63             if (node.sons[pos] == null) return false;
64             return search(word, node.sons[pos], cur+1, len);
65         }
66     }
67 }
68
69 // Your WordDictionary object will be instantiated and called as such:
70 // WordDictionary wordDictionary = new WordDictionary();
71 // wordDictionary.addWord("word");
72 // wordDictionary.search("pattern");
时间: 2024-11-04 18:33:09

Leetcode: Add and Search Word - Data structure design的相关文章

LeetCode——Add and Search Word - Data structure design

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 a

[LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

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

[LeetCode]Add and Search Word - Data structure design,解题报告

目录 目录 思路 思路 AC代码 思路 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 repr

LeetCode Add and Search Word - Data structure design (trie树)

题意:实现添加单词和查找单词的作用,即实现字典功能. 思路:'.' 可以代表一个任何小写字母,可能是".abc"或者"a.bc"或者"abc.",能应对这三种就没有问题了.在每个单词的尾字母上标上tag=1,代表从树根到此节点有一个单词.暂时想不到更快的办法. 1 class WordDictionary { 2 public: 3 WordDictionary(){ 4 tree=create(); 5 } 6 void addWord(str

[LeetCode][JavaScript]Add and Search Word - Data structure design

Add and Search Word - Data structure design 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 

【LeetCode】211. Add and Search Word - Data structure design

Add and Search Word - Data structure design 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 

Java for LeetCode 211 Add and Search Word - Data structure design

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.

(*medium)LeetCode 211.Add and Search Word - Data structure design

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

[LeetCode] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计

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