---
# System prepended metadata

title: 676. Implement Magic Dictionary
tags: [String]

---

---
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;
    }
}

