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
You may assume that all words are consist of lowercase letters
a-z
.public class WordDictionary {
TrieNode root = new TrieNode(' ');
// Adds a word into the data structure.
public void addWord(String word) {
TrieNode node = root;
for (int i = 0; i < word.length(); i++) {
int cid = word.charAt(i) - 'a';
if (node.children[cid] == null) {
node.children[cid] = new TrieNode(word.charAt(i));
}
node = node.children[cid];
}
node.isWord = true;
}
// 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) {
TrieNode node = root;
return searchHelper(word, root);
}
private boolean searchHelper(String word, TrieNode node) {
if (node == null) {
return false;
}
if (word.isEmpty()) {
return node != null && node.isWord;
}
if (word.charAt(0) == '.') {
for (char c = 'a'; c <= 'z'; c++) {
if (searchHelper(word.substring(1), node.children[c - 'a'])) {
return true;
}
}
return false;
} else {
char c = word.charAt(0);
return searchHelper(word.substring(1), node.children[c - 'a']);
}
}
public static class TrieNode {
char c;
boolean isWord;
TrieNode children[];
public TrieNode(char c) {
this.c = c;
this.isWord = false;
this.children = new TrieNode[26];
}
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
===============
public class WordDictionary {
TrieNode root = new TrieNode(' ');
// Adds a word into the data structure.
public void addWord(String word) {
TrieNode node = root;
for (int i = 0; i < word.length(); i++) {
int cid = word.charAt(i) - 'a';
if (node.children[cid] == null) {
node.children[cid] = new TrieNode(word.charAt(i));
}
node = node.children[cid];
}
node.isWord = true;
}
// Returns if the word is in the data structure. A word could
// contain the dot character '.' to represent any one letter.
private String word;
public boolean search(String word) {
this.word = word;
TrieNode node = root;
return searchHelper(0, root);
}
private boolean searchHelper(int cur, TrieNode node) {
if (node == null) {
return false;
}
if (cur == word.length()) {
return node != null && node.isWord;
}
if (word.charAt(cur) == '.') {
for (char c = 'a'; c <= 'z'; c++) {
if (searchHelper(cur + 1, node.children[c - 'a'])) {
return true;
}
}
return false;
} else {
char c = word.charAt(cur);
return searchHelper(cur + 1, node.children[c - 'a']);
}
}
public static class TrieNode {
char c;
boolean isWord;
TrieNode children[];
public TrieNode(char c) {
this.c = c;
this.isWord = false;
this.children = new TrieNode[26];
}
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
没有评论:
发表评论