290. 单词规律
小于 1 分钟
290. 单词规律简单
解法思路:哈希表
class Solution {
public boolean wordPattern(String pattern, String s) {
HashMap<Character, String> map = new HashMap<>();
HashSet<String> set = new HashSet<>();
String[] split = s.split(" ");
if(pattern.length() != split.length) return false;
for (int i = 0; i < pattern.length(); i++) {
if (!map.containsKey(pattern.charAt(i))) {
if (set.contains(split[i]))
return false;
map.put(pattern.charAt(i), split[i]);
set.add(split[i]);
} else if (!map.get(pattern.charAt(i)).equals(split[i])) {
return false;
}
}
return true;
}
}
Powered by Waline v2.15.5