该题有两种思路:
one:利用Trie树实现:
class WordDictionary {
private class Trie {
public boolean isWord;
public Trie[] children;
public Trie() {
this.isWord = false;
this.children = new Trie[26];
}
}
private Trie root;
/** Initialize your data structure here. */
public WordDictionary() {
root = new Trie();
}
/** Adds a word into the data structure. */
public void addWord(String word) {
Trie node = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (node.children[c - 'a'] == null) node.children[c - 'a'] = new Trie();
node = node.children[c - 'a'];
}
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) {
return search(word, 0, root);
}
// word may contain . for wild card matching
private boolean search(String word, int start, Trie node) {
if (start == word.length()) return node.isWord;
char c = word.charAt(start);
if (c == '.') {
for (int i = 0; i < 26; i++) {
if (node.children[i] != null) {
if (search(word, start + 1, node.children[i])) return true;
}
}
return false;
} else {
if (node.children[c - 'a'] == null) return false;
return search(word, start + 1, node.children[c - 'a']);
}
}
}
class WordDictionary {
public class TrieNode {
public TrieNode[] children = new TrieNode[26];
public String item = "";
}
private TrieNode root = new TrieNode();
public void addWord(String word) {
TrieNode node = root;
for (char c : word.toCharArray()) {
if (node.children[c - 'a'] == null) {
node.children[c - 'a'] = new TrieNode();
}
node = node.children[c - 'a'];
}
node.item = word;
}
public boolean search(String word) {
return match(word.toCharArray(), 0, root);
}
private boolean match(char[] chs, int k, TrieNode node) {
if (k == chs.length) return !node.item.equals("");
if (chs[k] != '.') {
return node.children[chs[k] - 'a'] != null && match(chs, k + 1, node.children[chs[k] - 'a']);
} else {
for (int i = 0; i < node.children.length; i++) {
if (node.children[i] != null) {
if (match(chs, k + 1, node.children[i])) {
return true;
}
}
}
}
return false;
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/
two:HashMap法:
//fastest solution
class WordDictionary {
private HashMap<Integer, List<String>> map;
/** Initialize your data structure here. */
public WordDictionary() {
map = new HashMap<>();
}
/** Adds a word into the data structure. */
public void addWord(String word) {
int length = word.length();
if(map.containsKey(length)){
map.get(length).add(word);
}else{
map.put(length, new ArrayList<>());
map.get(length).add(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) {
int length = word.length();
if(!map.containsKey(length)){
return false;
}else{
List<String> list = map.get(length);
for(String cur : list){
if(isSame(cur,word)) return true;
}
return false;
}
}
public boolean isSame(String s1, String s2){
int n = s1.length();
for(int i = 0; i < n; i++){
if(s2.charAt(i) == '.') continue;
else{
if(s1.charAt(i) != s2.charAt(i)) return false;
}
}
return true;
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/