-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution0290.java
More file actions
34 lines (29 loc) · 920 Bytes
/
Solution0290.java
File metadata and controls
34 lines (29 loc) · 920 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
31
32
33
34
package leetcode;
import java.util.HashMap;
/**
* @author xiangdotzhaoAtwoqutechcommacom
* @date 2019/12/15
*/
public class Solution0290 {
public boolean wordPattern(String pattern, String str) {
String[] strArr = str.split(" ");
if (pattern.length() != strArr.length) {
return false;
}
HashMap<Character, String> map = new HashMap<>();
HashMap<String, Character> reverseMap = new HashMap<>();
for (int i = 0; i < strArr.length; i++) {
char c = pattern.charAt(i);
if (!map.containsKey(c)) {
if (reverseMap.containsKey(strArr[i])) {
return false;
}
map.put(c, strArr[i]);
reverseMap.put(strArr[i], c);
} else if (!map.get(c).equals(strArr[i])) {
return false;
}
}
return true;
}
}