-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWordPattern.java
More file actions
26 lines (25 loc) · 758 Bytes
/
WordPattern.java
File metadata and controls
26 lines (25 loc) · 758 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public boolean wordPattern(String pattern, String s) {
String[] word=s.split(" ");
if(word.length!=pattern.length())
return false;
HashMap<Character,String> hm=new HashMap<>();
HashSet<String> hs=new HashSet<>();
for(int i=0;i<pattern.length();i++){
char ch=pattern.charAt(i);
if(hm.containsKey(ch)){
if(!word[i].equals(hm.get(ch)))
return false;
}
else{
if(hs.contains(word[i]))
return false;
else{
hs.add(word[i]);
hm.put(ch,word[i]);
}
}
}
return true;
}
}