12/16
水题
class Solution {
public boolean wordPattern(String pattern, String s) {
int n = pattern.length();
Map<Character, String> mp = new HashMap<Character, String>();
String[] str = s.split(" ");
int m = str.length;
if (m != n) {
return false;
}
for (int i = 0; i < m; i++) {
char ch=pattern.charAt(i);
if (mp.containsKey(ch)) {
if(!mp.get(ch).equals(str[i])) {
return false;
}
continue;
}
if(!mp.containsValue(str[i])) {
mp.put(ch, str[i]);
continue;
}
return false;
}
return true;
}
}