单词规律(简单)
2020年6月13日
题目来源:力扣
解题
- 哈希表
普通的哈希map问题,直接进行映射,不匹配就false
class Solution {
public boolean wordPattern(String pattern, String str) {
String[] stray=str.split(" ");
if(pattern.length()!=stray.length)
return false;
Map<String,Character> map=new HashMap<>();
for(int i=0;i<stray.length;i++){
if(!map.containsKey(stray[i])){
if(map.containsValue(pattern.charAt(i)))
return false;
map.put(stray[i],pattern.charAt(i));
}
if(pattern.charAt(i)!=map.get(stray[i]))
return false;
}
return true;
}
}