676は、魔法の辞書を実装します
魔法buildDictと辞書を実装し、メソッドを検索します。
buildDict方法について、あなたは辞書を構築するために、単語の非繰り返しの文字列が与えられます。
検索方法について、あなたは言葉を与えられます、そして、別の文字に単語の唯一の文字は、そのように形成された新しい単語が辞書に存在していることをあなたが構築するかどうかが決定されます。
例1:
Input: buildDict(["hello", "leetcode"]), Output: Null
Input: search("hello"), Output: False
Input: search("hhllo"), Output: True
Input: search("hell"), Output: False
Input: search("leetcoded"), Output: False
注意:
あなたは、すべての入力が小文字がAZであると仮定してもよいです。
競争を促進するために、テストで使用されるデータの量が少ないです。あなたは競争の終わりに、より効率的なアルゴリズムを考えることができます。
静的/クラス変数は、いくつかのテストケースのままになりますので、クラスで宣言MagicDictionaryクラス変数をリセットすることを忘れないでください。詳細はこちらをご覧ください。
class MagicDictionary {
List<String> list;
/** Initialize your data structure here. */
public MagicDictionary() {
list=new ArrayList();
}
/** Build a dictionary through a list of words */
public void buildDict(String[] dict) {
for(int i=0;i<dict.length;i++){
list.add(dict[i]);
}
}
/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
public boolean search(String word) {
int size=list.size();
int n=word.length();
for(int i=0;i<size;i++){
String cur=list.get(i);
if(cur.length()==n&¬SameOne(cur,word)){
return true;
}
}
return false;
}
public boolean notSameOne(String str1,String str2){
int n=str1.length();
int disCout=0;
for(int i=0;i<n;i++){
if(str1.charAt(i)!=str2.charAt(i)){
disCout++;
}
if(disCout>1) return false;
}
return disCout==1?true:false;
}
}
/**
* Your MagicDictionary object will be instantiated and called as such:
* MagicDictionary obj = new MagicDictionary();
* obj.buildDict(dict);
* boolean param_2 = obj.search(word);
*/