-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathWordPattern.java
41 lines (30 loc) · 927 Bytes
/
WordPattern.java
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
31
32
33
34
35
36
37
38
39
40
41
/*
Source: https://leetcode.com/problems/word-pattern/
Time: O(n * m), where n is the length of the string(pattern) and m is the length of a longest word in string(s)
Space: O(n), we need a HashMap of size equal to the length of the given string(pattern)
*/
class Solution {
public boolean wordPattern(String pattern, String s) {
String[] words = s.split(" ");
if(pattern.length() != words.length) {
return false;
}
HashMap<Character, String> map = new HashMap<>();
int len = words.length;
for(int i = 0; i < len; ++i) {
Character patternChar = pattern.charAt(i);
String word = words[i];
String val = map.get(patternChar);
if(val != null) {
if(!val.equals(word)) {
return false;
}
} else if(map.containsValue(word)) {
return false;
} else {
map.put(patternChar, word);
}
}
return true;
}
}