public class Solution {
public boolean wordPattern(String pattern, String str) {
String[]strs=str.split(" ");
if(pattern.length()!=strs.length)return false;
Map<Character,String>map=new HashMap<>();
Set<String>set=new HashSet<>();
for(int i=0;i<strs.length;i++){
char c=pattern.charAt(i);
if(map.containsKey(c)){
if(!map.get(c).equals(strs[i]))return false;
}
else{
if(set.contains(strs[i]))return false;
set.add(strs[i]);
map.put(c,strs[i]);
}
}
return true;
}
}
public boolean wordPattern(String pattern, String str) {
String[]strs=str.split(" ");
if(pattern.length()!=strs.length)return false;
Map<Character,String>map=new HashMap<>();
Set<String>set=new HashSet<>();
for(int i=0;i<strs.length;i++){
char c=pattern.charAt(i);
if(map.containsKey(c)){
if(!map.get(c).equals(strs[i]))return false;
}
else{
if(set.contains(strs[i]))return false;
set.add(strs[i]);
map.put(c,strs[i]);
}
}
return true;
}
}