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.

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");
时间: 2024-08-07 05:04:19

Java for LeetCode 211 Add and Search Word - Data structure design的相关文章

[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

[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

(*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

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

【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 

[LeetCode][JavaScript]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 

[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

[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