代码:
class Solution {
public boolean wordPattern(String pattern, String s) {
Map<Character,String> map1 = new HashMap<>();
Map<String,Character> map2 = new HashMap<>();
String[] words = s.split(" ");
int n = pattern.length();
int m = words.length;
if(m!=n)return false;
for(int i=0;i<n;i++){
char c = pattern.charAt(i);
String w = words[i];
if(map1.containsKey(c)){
if(!map1.get(c).equals(w))return false;
}else if(!map1.containsKey(c)&&!map2.containsKey(w)){
map1.put(c,w);
map2.put(w,c);
}else{
return false;
}
}
return true;
}
}