-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordPattern.java
More file actions
30 lines (29 loc) · 876 Bytes
/
wordPattern.java
File metadata and controls
30 lines (29 loc) · 876 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
29
30
class Solution {
public boolean wordPattern(String pattern, String s) {
int l1 = pattern.length();
String[] split = s.split(" ");
int l2 = split.length;
if(l1 != l2)
return false;
int index = 0;
HashMap<Character, String> charMap = new HashMap<>();
charMap.put(pattern.charAt(index), split[index]);
index++;
while(index < l2)
{
if(charMap.containsKey(pattern.charAt(index)))
{
if(!charMap.get(pattern.charAt(index)).equals(split[index]))
return false;
index++;
}
else{
if(charMap.containsValue(split[index]))
return false;
charMap.put(pattern.charAt(index), split[index]);
index++;
}
}
return true;
}
}