forked from surajr/CodingInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordPattern.java
More file actions
28 lines (24 loc) · 739 Bytes
/
wordPattern.java
File metadata and controls
28 lines (24 loc) · 739 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
27
28
class Solution {
public boolean wordPattern(String pattern, String str) {
String [] words = str.split(" ");
if(words.length != pattern.length())
return false;
HashMap<Character, String> map = new HashMap<>();
for(int i=0; i<words.length; i++)
{
char c = pattern.charAt(i);
if(map.containsKey(c))
{
if(!map.get(c).equals(words[i]))
return false;
}
else
{
if(map.containsValue(words[i]))
return false;
map.put(c, words[i]);
}
}
return true;
}
}