Skip to content

Commit 70d81f1

Browse files
WordPattern.java added
1 parent 7efeaa8 commit 70d81f1

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public boolean wordPattern(String pattern, String s) {
3+
String[] word=s.split(" ");
4+
if(word.length!=pattern.length())
5+
return false;
6+
HashMap<Character,String> hm=new HashMap<>();
7+
HashSet<String> hs=new HashSet<>();
8+
9+
for(int i=0;i<pattern.length();i++){
10+
char ch=pattern.charAt(i);
11+
if(hm.containsKey(ch)){
12+
if(!word[i].equals(hm.get(ch)))
13+
return false;
14+
}
15+
else{
16+
if(hs.contains(word[i]))
17+
return false;
18+
else{
19+
hs.add(word[i]);
20+
hm.put(ch,word[i]);
21+
}
22+
}
23+
}
24+
return true;
25+
}
26+
}

0 commit comments

Comments
 (0)