Skip to content

Commit d2f1726

Browse files
committed
Added Word Pattern
1 parent 9985cb3 commit d2f1726

4 files changed

Lines changed: 102 additions & 1 deletion

File tree

‎java/SUMMARY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* [Simplify Path](string/simplify-path.md)
6363
* [Length of Last Word](string/length-of-last-word.md)
6464
* [Isomorphic Strings](string/isomorphic-strings.md)
65+
* [Word Pattern](string/word-pattern.md)
6566
* [栈和队列](stack-and-queue/README.md)
6667
* [栈](stack-and-queue/stack/README.md)
6768
* [Min Stack](stack-and-queue/stack/min-stack.md)

‎java/code/word-pattern.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Word Pattern
2+
// Time Complexity: O(n), Space Complexity: O(n)
3+
public class Solution {
4+
public boolean wordPattern(String pattern, String str) {
5+
String[] words = str.split(" ");
6+
if (words.length != pattern.length()) return false;
7+
8+
final Map<Character, String> map1 = new HashMap<>();
9+
final Map<String, Character> map2 = new HashMap<>();
10+
for (int i = 0; i < words.length; ++i) {
11+
final Character key1 = pattern.charAt(i);
12+
if (map1.containsKey(key1)) {
13+
final String value = map1.get(key1);
14+
if (!value.equals(words[i])) return false;
15+
} else {
16+
map1.put(key1, words[i]);
17+
}
18+
19+
final String key2 = words[i];
20+
if (map2.containsKey(key2)) {
21+
final char value = map2.get(key2);
22+
if (value != pattern.charAt(i)) return false;
23+
} else {
24+
map2.put(key2, pattern.charAt(i));
25+
}
26+
}
27+
return true;
28+
}
29+
}

‎java/string/isomorphic-strings.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## isomorphic-strings
1+
## Isomorphic Strings
22

33
### 描述
44

@@ -60,3 +60,8 @@ public class Solution {
6060
}
6161
```
6262
{% endif %}
63+
64+
65+
### 相关题目
66+
67+
* [Word Pattern](word-pattern.md)

‎java/string/word-pattern.md‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## Word Pattern
2+
3+
4+
### 描述
5+
6+
Given a `pattern` and a string `str`, find if `str` follows the same pattern.
7+
8+
Here follow means a full match, such that there is a bijection between a letter in `pattern` and a non-empty word in `str`.
9+
10+
**Examples**:
11+
12+
1. pattern = `"abba"`, str = `"dog cat cat dog"` should return true.
13+
1. pattern = `"abba"`, str = `"dog cat cat fish"` should return false.
14+
1. pattern = `"aaaa"`, str = `"dog cat cat dog"` should return false.
15+
1. pattern = `"abba"`, str = `"dog dog dog dog"` should return false.
16+
17+
**Notes**:
18+
19+
You may assume `pattern` contains only lowercase letters, and `str` contains lowercase letters separated by a single space.
20+
21+
22+
### 分析
23+
24+
本题跟 "Isomorphic Strings" 很类似,用两个HashMap, 记录从字符到字符串和字符串到字符的映射。
25+
26+
27+
### 代码
28+
29+
{% if book.java %}
30+
```java
31+
// Word Pattern
32+
// Time Complexity: O(n), Space Complexity: O(n)
33+
public class Solution {
34+
public boolean wordPattern(String pattern, String str) {
35+
String[] words = str.split(" ");
36+
if (words.length != pattern.length()) return false;
37+
38+
final Map<Character, String> map1 = new HashMap<>();
39+
final Map<String, Character> map2 = new HashMap<>();
40+
for (int i = 0; i < words.length; ++i) {
41+
final Character key1 = pattern.charAt(i);
42+
if (map1.containsKey(key1)) {
43+
final String value = map1.get(key1);
44+
if (!value.equals(words[i])) return false;
45+
} else {
46+
map1.put(key1, words[i]);
47+
}
48+
49+
final String key2 = words[i];
50+
if (map2.containsKey(key2)) {
51+
final char value = map2.get(key2);
52+
if (value != pattern.charAt(i)) return false;
53+
} else {
54+
map2.put(key2, pattern.charAt(i));
55+
}
56+
}
57+
return true;
58+
}
59+
}
60+
```
61+
{% endif %}
62+
63+
64+
### 相关题目
65+
66+
* [Isomorphic Strings](isomorphic-strings.md)

0 commit comments

Comments
 (0)