--- title: 676. Implement Magic Dictionary tags: String description: share source code. --- # 676. Implement Magic Dictionary ```java class MagicDictionary { Map<Integer, List<String>> map; public MagicDictionary() { map = new HashMap<>(); } public void buildDict(String[] dictionary) { for(String dict : dictionary){ int len = dict.length(); map.putIfAbsent(len, new ArrayList<>()); map.get(len).add(dict); } } public boolean search(String searchWord) { List<String> wordBanck = map.getOrDefault(searchWord.length(), new ArrayList<>()); for(String word: wordBanck){ if(check(word, searchWord)){ return true; } } return false; } public boolean check(String a, String b){ if(a.length() != b.length()){ return false; } if(a.equals(b)){ return false; } int cnt = 0; for(int i = 0; i < a.length() ; i++){ if(a.charAt(i) != b.charAt(i) ){ cnt++; } } return cnt == 1; } }
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up