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