[LC] 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.

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

 1 class WordDictionary {
 2     private TrieNode root;
 3
 4     /** Initialize your data structure here. */
 5     public WordDictionary() {
 6         root = new TrieNode();
 7     }
 8
 9     /** Adds a word into the data structure. */
10     public void addWord(String word) {
11         TrieNode cur = root;
12         for (char c: word.toCharArray()) {
13             if (cur.children[c - ‘a‘] == null) {
14                 cur.children[c - ‘a‘] = new TrieNode();
15             }
16             cur = cur.children[c - ‘a‘];
17         }
18         cur.isWord = true;
19     }
20
21     /** Returns if the word is in the data structure. A word could contain the dot character ‘.‘ to represent any one letter. */
22     public boolean search(String word) {
23         return helper(word, root, 0);
24     }
25
26     private boolean helper(String word, TrieNode root, int level) {
27         if (level == word.length()) {
28             return root.isWord;
29         }
30         char cur = word.charAt(level);
31         if (cur == ‘.‘) {
32             for (TrieNode child : root.children) {
33                 if (child != null && helper(word, child, level + 1)) {
34                     return true;
35                 }
36             }
37             return false;
38         } else {
39             return root.children[cur - ‘a‘] != null && helper(word, root.children[cur - ‘a‘], level + 1);
40         }
41     }
42 }
43
44 class TrieNode {
45     TrieNode[] children;
46     boolean isWord;
47
48     public TrieNode() {
49         children = new TrieNode[26];
50         isWord = false;
51     }
52 }
53
54 /**
55  * Your WordDictionary object will be instantiated and called as such:
56  * WordDictionary obj = new WordDictionary();
57  * obj.addWord(word);
58  * boolean param_2 = obj.search(word);
59  */

原文地址:https://www.cnblogs.com/xuanlu/p/12005192.html

时间: 2024-10-03 10:02:56

[LC] 211. Add and Search Word - Data structure design的相关文章

【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 

[leedcode 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(字典树)

题目 字典树. class WordDictionary { public: int map[100005][26]; int tag[100005]; int num; /** Initialize your data structure here. */ WordDictionary() { memset(map,0,sizeof(map)); memset(tag,0,sizeof(tag)); num=0; } /** Adds a word into the data structur

[LeetCode] 211. Add and Search Word - Data structure design Java

题目: 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 le

211. Add and Search Word - Data structure design

就是trie 1 public class WordDictionary { 2 public class TrieNode { 3 public TrieNode[] child; 4 public char curChar; 5 public boolean isLeaf; 6 7 public TrieNode() { 8 child = new TrieNode[26]; 9 isLeaf = false; 10 } 11 } 12 13 TrieNode root; 14 15 pub

[leetcode trie]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

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.

LeetCode 211. Add and Search Word - Data structure design

原题 设计一个包含下面两个操作的数据结构:addWord(word), search(word)addWord(word)会在数据结构中添加一个单词.而search(word)则支持普通的单词查询或是只包含. 和a-z的简易正则表达式的查询.一个 . 可以代表一个任何的字母. 样例 addWord("bad") addWord("dad") addWord("mad") search("pad") // return fals