Tuesday, September 3, 2019

290. word-pattern

290. word-pattern
  • 문제 이해가 문제보다 더 어려운 문제
bijection :  전단사 
class Solution {
    public boolean wordPattern(String pattern, String str) {
        
        String[] map = new String[30];
        char [] patternArr  = pattern.toCharArray();
        String[] strArr = str.split(" ");

        Set<String> used = new HashSet<>();

        if(patternArr.length != strArr.length){
            return false;
        }

        for(int i=0; i<patternArr.length; i++){
            String value = map[patternArr[i]-'a'] ;
            if(value == null){
                if(used .contains(strArr[i])){
                    return false;
                }
                used.add(strArr[i]);
                map[patternArr[i]-'a']  = strArr[i];
            }else{
                if(!map[patternArr[i]-'a'].equals(strArr[i])){
                    return false;
                }
            }
        }

        return true;
    }
}

No comments:

Post a Comment