676. Implement Magic Dictionary
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//pseudo code
List<String> words = new ArrayList<>();
public void build(String[] dict) {
for (String s : dict) words.add(s);
}
public boolean search(String word) {
for (String s : words) {
if (word.length() != s.length()) continue;
int count = 0;
for (int i = 0; i < word.length(); i++) {
char a = word.charAt(i);
char b = word.charAt(i);
if (a != b) count++;
if (count > 1) break;
}
if (count == 1) return true;
}
return false;
}Last updated