您的位置:首页 > 其它

Add and Search Word - Data structure design 解答

2015-10-13 10:20 357 查看

Question

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
.

Solution

This kind of problem (searching a String) can be solved by Trie.

We use DFS to implement search. Notice corner cases.

When we choose to implement it by recursion, think one step each time.

class TrieNode {
public char value;
public boolean isLeaf;
public HashMap<Character, TrieNode> children;

public TrieNode(char c) {
value = c;
children = new HashMap<Character, TrieNode>();
isLeaf = false;
}
}

public class WordDictionary {
public TrieNode root;

public WordDictionary() {
root = new TrieNode('!');
}

// Adds a word into the data structure.
public void addWord(String word) {
TrieNode currentNode = root;
for (int i = 0; i < word.length(); i++) {
char tmp = word.charAt(i);
HashMap<Character, TrieNode> children = currentNode.children;
TrieNode nextNode;
if (children.containsKey(tmp)) {
nextNode = children.get(tmp);
} else {
nextNode = new TrieNode(tmp);
children.put(tmp, nextNode);
}
currentNode = nextNode;
// Check whether it's the last character
if (i == word.length() - 1)
currentNode.isLeaf = 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) {
return dfsSearch(word, 0, root);
}

private boolean dfsSearch(String word, int index, TrieNode prevNode) {
// If prevNode is null but word has not been completely traversed, return fasle
if (prevNode == null) {
return false;
}

// If word has been completely traversed, check whether tree is at bottom
if (index == word.length()) {
return prevNode.isLeaf;
}
char target = word.charAt(index);
HashMap<Character, TrieNode> currentMap = prevNode.children;

if (target != '.') {
if (!currentMap.containsKey(target))
return false;
else
return dfsSearch(word, index + 1, currentMap.get(target));
} else {
boolean result = false;
for (Character key : currentMap.keySet()) {
if (dfsSearch(word, index + 1, currentMap.get(key))) {
result = true;
}
}
return result;
}
}
}

// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: