標籤:
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
解題思路:
參考之前的Java for LeetCode 208 Implement Trie (Prefix Tree) 修改下即可,JAVA實現如下:
public class WordDictionary extends Trie {public void addWord(String word) {super.insert(word);}// 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) {if (word == null || word.length() == 0)return false;return search(word, 0, root);}public boolean search(String word, int depth, TrieNode node) {if (depth == word.length() - 1) {if (word.charAt(depth) != ‘.‘) {if (node.son[word.charAt(depth) - ‘a‘] != null) {node = node.son[word.charAt(depth) - ‘a‘];return node.isEnd;} elsereturn false;}for (int i = 0; i < 26; i++) {if (node.son[i] != null) {TrieNode ason = node.son[i];if (ason.isEnd)return true;}}return false;}if (word.charAt(depth) != ‘.‘) {if (node.son[word.charAt(depth) - ‘a‘] != null) {node = node.son[word.charAt(depth) - ‘a‘];return search(word, depth + 1, node);} elsereturn false;}for (int i = 0; i < 26; i++) {if (node.son[i] != null) {TrieNode ason = node.son[i];if (search(word, depth + 1, ason))return true;}}return false;}}class TrieNode {// Initialize your data structure here.int num;// 有多少單詞通過這個節點,即節點字元出現的次數TrieNode[] son;// 所有的兒子節點boolean isEnd;// 是不是最後一個節點char val;// 節點的值TrieNode() {this.num = 1;this.son = new TrieNode[26];this.isEnd = false;}}class Trie {protected TrieNode root;public Trie() {root = new TrieNode();}public void insert(String word) {if (word == null || word.length() == 0)return;TrieNode node = this.root;char[] letters = word.toCharArray();for (int i = 0; i < word.length(); i++) {int pos = letters[i] - ‘a‘;if (node.son[pos] == null) {node.son[pos] = new TrieNode();node.son[pos].val = letters[i];} else {node.son[pos].num++;}node = node.son[pos];}node.isEnd = true;}// Returns if the word is in the trie.public boolean search(String word) {if (word == null || word.length() == 0) {return false;}TrieNode node = root;char[] letters = word.toCharArray();for (int i = 0; i < word.length(); i++) {int pos = letters[i] - ‘a‘;if (node.son[pos] != null) {node = node.son[pos];} else {return false;}}return node.isEnd;}// Returns if there is any word in the trie// that starts with the given prefix.public boolean startsWith(String prefix) {if (prefix == null || prefix.length() == 0) {return false;}TrieNode node = root;char[] letters = prefix.toCharArray();for (int i = 0; i < prefix.length(); i++) {int pos = letters[i] - ‘a‘;if (node.son[pos] != null) {node = node.son[pos];} else {return false;}}return true;}}// Your Trie object will be instantiated and called as such:// Trie trie = new Trie();// trie.insert("somestring");// trie.search("key");
Java for LeetCode 211 Add and Search Word - Data structure design