Java for LeetCode 211 Add and Search Word

 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;
				} else
					return 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);
			} else
				return 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");
原文地址:https://www.cnblogs.com/tonyluis/p/4564310.html