-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathWord Pattern.java
executable file
·105 lines (89 loc) · 3.15 KB
/
Word Pattern.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
E
每个char代表一个pattern。用HashMap<char, str>.
但不够,如果a也match dog, b也match dog, 纠错了。比如pattern = "abba", str = "dog dog dog dog"。
因此第二个HashMap<str, char> 反过来。
确保pattern和str一一对应。
```
/*
Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
Examples:
pattern = "abba", str = "dog cat cat dog" should return true.
pattern = "abba", str = "dog cat cat fish" should return false.
pattern = "aaaa", str = "dog cat cat dog" should return false.
pattern = "abba", str = "dog dog dog dog" should return false.
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
Credits:
Special thanks to @minglotus6 for adding this problem and creating all test cases.
Hide Company Tags Dropbox
Hide Tags Hash Table
Hide Similar Problems (E) Isomorphic Strings (H) Word Pattern II
*/
/*
3.1.2016 recap.
HashMap, one to one mapping
*/
public class Solution {
public boolean wordPattern(String pattern, String str) {
if (pattern == null || pattern.length() == 0 || str == null || str.length() == 0) {
return false;
}
String[] strArr = str.split(" ");
if (pattern.length() != strArr.length) {
return false;
}
HashMap<Character, String> map = new HashMap<Character, String>();
for (int i = 0; i < pattern.length(); i++) {
char c = pattern.charAt(i);
String s = strArr[i];
if (!map.containsKey(c)) {
if (map.containsValue(s)) {
return false;
}
map.put(c, s);
} else if (!map.get(c).equals(s)) {
return false;
}
}
return true;
}
}
/*
Thoughts:
2 HashMap<char, string>, HashMap<String, char> double check
*/
public class Solution {
public boolean wordPattern(String pattern, String str) {
if (pattern != null && str != null && pattern.length() == 0 && str.length() == 0) {
return true;
}
if (pattern == null || pattern.length() == 0 || str == null || str.length() == 0) {
return false;
}
String[] strArr = str.split(" ");
if (pattern.length() != strArr.length) {
return false;
}
HashMap<Character, String> map = new HashMap<Character, String>();
HashMap<String, Character> mapStr = new HashMap<String, Character>();
for (int i = 0; i < strArr.length; i++){
if (!map.containsKey(pattern.charAt(i))) {
map.put(pattern.charAt(i), strArr[i]);
} else {
if (!map.get(pattern.charAt(i)).equals(strArr[i])) {
return false;
}
}
if (!mapStr.containsKey(strArr[i])) {
mapStr.put(strArr[i], pattern.charAt(i));
} else {
if (mapStr.get(strArr[i]) != pattern.charAt(i)) {
return false;
}
}
}
return true;
}
}
```