Skip to content

Commit 76e3417

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0290
1 parent 02c5c2b commit 76e3417

File tree

4 files changed

+106
-15
lines changed

4 files changed

+106
-15
lines changed

solution/0200-0299/0290.Word Pattern/README.md

+38-2
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,51 @@
4343
<!-- 这里可写当前语言的特殊实现逻辑 -->
4444

4545
```python
46-
46+
class Solution:
47+
def wordPattern(self, pattern: str, s: str) -> bool:
48+
ch2str, str2ch = {}, {}
49+
ss = s.split(' ')
50+
n = len(pattern)
51+
if n != len(ss):
52+
return False
53+
for i in range(n):
54+
if ch2str.get(pattern[i]) is not None and ch2str.get(pattern[i]) != ss[i]:
55+
return False
56+
if str2ch.get(ss[i]) is not None and str2ch.get(ss[i]) != pattern[i]:
57+
return False
58+
ch2str[pattern[i]] = ss[i]
59+
str2ch[ss[i]] = pattern[i]
60+
return True
4761
```
4862

4963
### **Java**
5064

5165
<!-- 这里可写当前语言的特殊实现逻辑 -->
5266

5367
```java
54-
68+
class Solution {
69+
public boolean wordPattern(String pattern, String s) {
70+
Map<Character, String> ch2str = new HashMap<>();
71+
Map<String, Character> str2ch = new HashMap<>();
72+
String[] ss = s.split(" ");
73+
int n = pattern.length();
74+
if (n != ss.length) {
75+
return false;
76+
}
77+
for (int i = 0; i < n; ++i) {
78+
char ch = pattern.charAt(i);
79+
if (ch2str.containsKey(ch) && !ch2str.get(ch).equals(ss[i])) {
80+
return false;
81+
}
82+
if (str2ch.containsKey(ss[i]) && !str2ch.get(ss[i]).equals(ch)) {
83+
return false;
84+
}
85+
ch2str.put(ch, ss[i]);
86+
str2ch.put(ss[i], ch);
87+
}
88+
return true;
89+
}
90+
}
5591
```
5692

5793
### **...**

solution/0200-0299/0290.Word Pattern/README_EN.md

+38-2
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,49 @@ You may assume <code>pattern</code> contains only lowercase letters, and <code>s
5151
### **Python3**
5252

5353
```python
54-
54+
class Solution:
55+
def wordPattern(self, pattern: str, s: str) -> bool:
56+
ch2str, str2ch = {}, {}
57+
ss = s.split(' ')
58+
n = len(pattern)
59+
if n != len(ss):
60+
return False
61+
for i in range(n):
62+
if ch2str.get(pattern[i]) is not None and ch2str.get(pattern[i]) != ss[i]:
63+
return False
64+
if str2ch.get(ss[i]) is not None and str2ch.get(ss[i]) != pattern[i]:
65+
return False
66+
ch2str[pattern[i]] = ss[i]
67+
str2ch[ss[i]] = pattern[i]
68+
return True
5569
```
5670

5771
### **Java**
5872

5973
```java
60-
74+
class Solution {
75+
public boolean wordPattern(String pattern, String s) {
76+
Map<Character, String> ch2str = new HashMap<>();
77+
Map<String, Character> str2ch = new HashMap<>();
78+
String[] ss = s.split(" ");
79+
int n = pattern.length();
80+
if (n != ss.length) {
81+
return false;
82+
}
83+
for (int i = 0; i < n; ++i) {
84+
char ch = pattern.charAt(i);
85+
if (ch2str.containsKey(ch) && !ch2str.get(ch).equals(ss[i])) {
86+
return false;
87+
}
88+
if (str2ch.containsKey(ss[i]) && !str2ch.get(ss[i]).equals(ch)) {
89+
return false;
90+
}
91+
ch2str.put(ch, ss[i]);
92+
str2ch.put(ss[i], ch);
93+
}
94+
return true;
95+
}
96+
}
6197
```
6298

6399
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
class Solution {
2-
public boolean wordPattern(String pattern, String str) {
3-
String[] split = str.split(" ");
4-
if (pattern.length() != split.length) {
2+
public boolean wordPattern(String pattern, String s) {
3+
Map<Character, String> ch2str = new HashMap<>();
4+
Map<String, Character> str2ch = new HashMap<>();
5+
String[] ss = s.split(" ");
6+
int n = pattern.length();
7+
if (n != ss.length) {
58
return false;
69
}
7-
Map<Character, Integer> map1 = new HashMap<>();
8-
Map<String, Integer> map2 = new HashMap<>();
9-
for (int i = 0; i < pattern.length(); ++i) {
10-
char c = pattern.charAt(i);
11-
if (!map1.getOrDefault(c, -1).equals(map2.getOrDefault(split[i], -1))) {
10+
for (int i = 0; i < n; ++i) {
11+
char ch = pattern.charAt(i);
12+
if (ch2str.containsKey(ch) && !ch2str.get(ch).equals(ss[i])) {
1213
return false;
1314
}
14-
map1.put(c, i);
15-
map2.put(split[i], i);
15+
if (str2ch.containsKey(ss[i]) && !str2ch.get(ss[i]).equals(ch)) {
16+
return false;
17+
}
18+
ch2str.put(ch, ss[i]);
19+
str2ch.put(ss[i], ch);
1620
}
1721
return true;
1822
}
19-
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def wordPattern(self, pattern: str, s: str) -> bool:
3+
ch2str, str2ch = {}, {}
4+
ss = s.split(' ')
5+
n = len(pattern)
6+
if n != len(ss):
7+
return False
8+
for i in range(n):
9+
if ch2str.get(pattern[i]) is not None and ch2str.get(pattern[i]) != ss[i]:
10+
return False
11+
if str2ch.get(ss[i]) is not None and str2ch.get(ss[i]) != pattern[i]:
12+
return False
13+
ch2str[pattern[i]] = ss[i]
14+
str2ch[ss[i]] = pattern[i]
15+
return True

0 commit comments

Comments
 (0)