Given an array strings, determine whether it follows the sequence given in the patterns array. In other words, there should be no i and j for which strings[i] = strings[j] and patterns[i] ≠ patterns[j] or for which strings[i] ≠ strings[j] and patterns[i] = patterns[j].

Example

Input/Output

풀이

boolean solution(String[] strings, String[] patterns) {
    // 패턴 = 문자열 의 HashMap 자료구조로 저장
    LinkedHashMap <String, String> map = new LinkedHashMap<>();
    
    for (int i = 0; i < strings.length; i ++) {
        // 패턴 = key
        String pattern = patterns[i];
        // 문자열 = value
        String str = strings[i];
        
        // 해당 패턴의 키가 있을 때
        if (map.containsKey(pattern)) {
            // 해당 키의 값이 현재 문자열과 다르다면 거짓
            if (!map.get(pattern).equals(str)) return false;
        } 
        // 해당 패턴의 키가 없을 때
        else {
            // 자료구조에 해당 문자열이 값으로 존재하면 거짓
            if (map.containsValue(str)) return false;
            
            map.put(pattern, str);
        }
    }
    
    return true;
}