Skip to content

Commit 1671062

Browse files
Word Pattern Java Solution added (#9)
* MinArrowsToBurstBalloons.java added * Problem Link added * Delete Problem-Link, Approach.md * MinArrowsToBurstBalloons.java added * Deleted duplicate directory * StrToIntAtoi.java added * WordPattern.java added Co-authored-by: Achal Hingrajiya <67894945+Achal-Hingrajiya@users.noreply.github.com>
1 parent 2fc84d1 commit 1671062

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)