Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit adacbb2

Browse files
committed
feat: add solutions to lc problems: No.0290,0645,0763,2042+
1 parent 6ca7bc1 commit adacbb2

File tree

55 files changed

+4149
-844
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+4149
-844
lines changed

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

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
<p><strong>说明:</strong><br>
3434
你可以假设&nbsp;<code>pattern</code>&nbsp;只包含小写字母,&nbsp;<code>str</code>&nbsp;包含了由单个空格分隔的小写字母。&nbsp; &nbsp;&nbsp;</p>
3535

36-
3736
## 解法
3837

3938
<!-- 这里可写通用的实现逻辑 -->
@@ -47,18 +46,18 @@
4746
```python
4847
class Solution:
4948
def wordPattern(self, pattern: str, s: str) -> bool:
50-
ch2str, str2ch = {}, {}
51-
ss = s.split(' ')
49+
s = s.split(' ')
5250
n = len(pattern)
53-
if n != len(ss):
51+
if n != len(s):
5452
return False
53+
c2str, str2c = collections.defaultdict(), collections.defaultdict()
5554
for i in range(n):
56-
if ch2str.get(pattern[i]) is not None and ch2str.get(pattern[i]) != ss[i]:
55+
k, v = pattern[i], s[i]
56+
if k in c2str and c2str[k] != v:
5757
return False
58-
if str2ch.get(ss[i]) isnotNoneand str2ch.get(ss[i]) != pattern[i]:
58+
if v in str2c and str2c[v] != k:
5959
return False
60-
ch2str[pattern[i]] = ss[i]
61-
str2ch[ss[i]] = pattern[i]
60+
c2str[k], str2c[v] = v, k
6261
return True
6362
```
6463

@@ -69,23 +68,24 @@ class Solution:
6968
```java
7069
class Solution {
7170
public boolean wordPattern(String pattern, String s) {
72-
Map<Character, String> ch2str = new HashMap<>();
73-
Map<String, Character> str2ch = new HashMap<>();
7471
String[] ss = s.split(" ");
7572
int n = pattern.length();
7673
if (n != ss.length) {
7774
return false;
7875
}
76+
Map<Character, String> c2str = new HashMap<>();
77+
Map<String, Character> str2c = new HashMap<>();
7978
for (int i = 0; i < n; ++i) {
80-
char ch = pattern.charAt(i);
81-
if (ch2str.containsKey(ch) && !ch2str.get(ch).equals(ss[i])) {
79+
char k = pattern.charAt(i);
80+
String v = ss[i];
81+
if (c2str.containsKey(k) && !Objects.equals(c2str.get(k), v)) {
8282
return false;
8383
}
84-
if (str2ch.containsKey(ss[i]) && !str2ch.get(ss[i]).equals(ch)) {
84+
if (str2c.containsKey(v) && !Objects.equals(str2c.get(v), k)) {
8585
return false;
8686
}
87-
ch2str.put(ch, ss[i]);
88-
str2ch.put(ss[i], ch);
87+
c2str.put(k, v);
88+
str2c.put(v, k);
8989
}
9090
return true;
9191
}
@@ -113,6 +113,59 @@ function wordPattern(pattern: string, s: string): boolean {
113113
};
114114
```
115115

116+
### **C++**
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
bool wordPattern(string pattern, string s) {
122+
istringstream is(s);
123+
vector<string> ss;
124+
while (is >> s) ss.push_back(s);
125+
int n = pattern.size();
126+
if (n != ss.size()) return false;
127+
128+
unordered_map<char, string> c2str;
129+
unordered_map<string, char> str2c;
130+
for (int i = 0; i < n; ++i)
131+
{
132+
char k = pattern[i];
133+
string v = ss[i];
134+
if (c2str.count(k) && c2str[k] != v) return false;
135+
if (str2c.count(v) && str2c[v] != k) return false;
136+
c2str[k] = v;
137+
str2c[v] = k;
138+
}
139+
return true;
140+
}
141+
};
142+
```
143+
144+
### **Go**
145+
146+
```go
147+
func wordPattern(pattern string, s string) bool {
148+
ss := strings.Split(s, " ")
149+
n := len(pattern)
150+
if n != len(ss) {
151+
return false
152+
}
153+
c2str := make(map[byte]string)
154+
str2c := make(map[string]byte)
155+
for i := 0; i < n; i++ {
156+
k, v := pattern[i], ss[i]
157+
if c2str[k] != "" && c2str[k] != v {
158+
return false
159+
}
160+
if str2c[v] > 0 && str2c[v] != k {
161+
return false
162+
}
163+
c2str[k], str2c[v] = v, k
164+
}
165+
return true
166+
}
167+
```
168+
116169
### **...**
117170

118171
```

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

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,18 @@
5959
```python
6060
class Solution:
6161
def wordPattern(self, pattern: str, s: str) -> bool:
62-
ch2str, str2ch = {}, {}
63-
ss = s.split(' ')
62+
s = s.split(' ')
6463
n = len(pattern)
65-
if n != len(ss):
64+
if n != len(s):
6665
return False
66+
c2str, str2c = collections.defaultdict(), collections.defaultdict()
6767
for i in range(n):
68-
if ch2str.get(pattern[i]) is not None and ch2str.get(pattern[i]) != ss[i]:
68+
k, v = pattern[i], s[i]
69+
if k in c2str and c2str[k] != v:
6970
return False
70-
if str2ch.get(ss[i]) isnotNoneand str2ch.get(ss[i]) != pattern[i]:
71+
if v in str2c and str2c[v] != k:
7172
return False
72-
ch2str[pattern[i]] = ss[i]
73-
str2ch[ss[i]] = pattern[i]
73+
c2str[k], str2c[v] = v, k
7474
return True
7575
```
7676

@@ -79,23 +79,24 @@ class Solution:
7979
```java
8080
class Solution {
8181
public boolean wordPattern(String pattern, String s) {
82-
Map<Character, String> ch2str = new HashMap<>();
83-
Map<String, Character> str2ch = new HashMap<>();
8482
String[] ss = s.split(" ");
8583
int n = pattern.length();
8684
if (n != ss.length) {
8785
return false;
8886
}
87+
Map<Character, String> c2str = new HashMap<>();
88+
Map<String, Character> str2c = new HashMap<>();
8989
for (int i = 0; i < n; ++i) {
90-
char ch = pattern.charAt(i);
91-
if (ch2str.containsKey(ch) && !ch2str.get(ch).equals(ss[i])) {
90+
char k = pattern.charAt(i);
91+
String v = ss[i];
92+
if (c2str.containsKey(k) && !Objects.equals(c2str.get(k), v)) {
9293
return false;
9394
}
94-
if (str2ch.containsKey(ss[i]) && !str2ch.get(ss[i]).equals(ch)) {
95+
if (str2c.containsKey(v) && !Objects.equals(str2c.get(v), k)) {
9596
return false;
9697
}
97-
ch2str.put(ch, ss[i]);
98-
str2ch.put(ss[i], ch);
98+
c2str.put(k, v);
99+
str2c.put(v, k);
99100
}
100101
return true;
101102
}
@@ -123,6 +124,59 @@ function wordPattern(pattern: string, s: string): boolean {
123124
};
124125
```
125126

127+
### **C++**
128+
129+
```cpp
130+
class Solution {
131+
public:
132+
bool wordPattern(string pattern, string s) {
133+
istringstream is(s);
134+
vector<string> ss;
135+
while (is >> s) ss.push_back(s);
136+
int n = pattern.size();
137+
if (n != ss.size()) return false;
138+
139+
unordered_map<char, string> c2str;
140+
unordered_map<string, char> str2c;
141+
for (int i = 0; i < n; ++i)
142+
{
143+
char k = pattern[i];
144+
string v = ss[i];
145+
if (c2str.count(k) && c2str[k] != v) return false;
146+
if (str2c.count(v) && str2c[v] != k) return false;
147+
c2str[k] = v;
148+
str2c[v] = k;
149+
}
150+
return true;
151+
}
152+
};
153+
```
154+
155+
### **Go**
156+
157+
```go
158+
func wordPattern(pattern string, s string) bool {
159+
ss := strings.Split(s, " ")
160+
n := len(pattern)
161+
if n != len(ss) {
162+
return false
163+
}
164+
c2str := make(map[byte]string)
165+
str2c := make(map[string]byte)
166+
for i := 0; i < n; i++ {
167+
k, v := pattern[i], ss[i]
168+
if c2str[k] != "" && c2str[k] != v {
169+
return false
170+
}
171+
if str2c[v] > 0 && str2c[v] != k {
172+
return false
173+
}
174+
c2str[k], str2c[v] = v, k
175+
}
176+
return true
177+
}
178+
```
179+
126180
### **...**
127181

128182
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
bool wordPattern(string pattern, string s) {
4+
istringstream is(s);
5+
vector<string> ss;
6+
while (is >> s) ss.push_back(s);
7+
int n = pattern.size();
8+
if (n != ss.size()) return false;
9+
10+
unordered_map<char, string> c2str;
11+
unordered_map<string, char> str2c;
12+
for (int i = 0; i < n; ++i)
13+
{
14+
char k = pattern[i];
15+
string v = ss[i];
16+
if (c2str.count(k) && c2str[k] != v) return false;
17+
if (str2c.count(v) && str2c[v] != k) return false;
18+
c2str[k] = v;
19+
str2c[v] = k;
20+
}
21+
return true;
22+
}
23+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func wordPattern(pattern string, s string) bool {
2+
ss := strings.Split(s, " ")
3+
n := len(pattern)
4+
if n != len(ss) {
5+
return false
6+
}
7+
c2str := make(map[byte]string)
8+
str2c := make(map[string]byte)
9+
for i := 0; i < n; i++ {
10+
k, v := pattern[i], ss[i]
11+
if c2str[k] != "" && c2str[k] != v {
12+
return false
13+
}
14+
if str2c[v] > 0 && str2c[v] != k {
15+
return false
16+
}
17+
c2str[k], str2c[v] = v, k
18+
}
19+
return true
20+
}

‎solution/0200-0299/0290.Word Pattern/Solution.java‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
class Solution {
22
public boolean wordPattern(String pattern, String s) {
3-
Map<Character, String> ch2str = new HashMap<>();
4-
Map<String, Character> str2ch = new HashMap<>();
53
String[] ss = s.split(" ");
64
int n = pattern.length();
75
if (n != ss.length) {
86
return false;
97
}
8+
Map<Character, String> c2str = new HashMap<>();
9+
Map<String, Character> str2c = new HashMap<>();
1010
for (int i = 0; i < n; ++i) {
11-
char ch = pattern.charAt(i);
12-
if (ch2str.containsKey(ch) && !ch2str.get(ch).equals(ss[i])) {
11+
char k = pattern.charAt(i);
12+
String v = ss[i];
13+
if (c2str.containsKey(k) && !Objects.equals(c2str.get(k), v)) {
1314
return false;
1415
}
15-
if (str2ch.containsKey(ss[i]) && !str2ch.get(ss[i]).equals(ch)) {
16+
if (str2c.containsKey(v) && !Objects.equals(str2c.get(v), k)) {
1617
return false;
1718
}
18-
ch2str.put(ch, ss[i]);
19-
str2ch.put(ss[i], ch);
19+
c2str.put(k, v);
20+
str2c.put(v, k);
2021
}
2122
return true;
2223
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Solution:
22
def wordPattern(self, pattern: str, s: str) -> bool:
3-
ch2str, str2ch = {}, {}
4-
ss = s.split(' ')
3+
s = s.split(' ')
54
n = len(pattern)
6-
if n != len(ss):
5+
if n != len(s):
76
return False
7+
c2str, str2c = collections.defaultdict(), collections.defaultdict()
88
for i in range(n):
9-
if ch2str.get(pattern[i]) is not None and ch2str.get(pattern[i]) != ss[i]:
9+
k, v = pattern[i], s[i]
10+
if k in c2str and c2str[k] != v:
1011
return False
11-
if str2ch.get(ss[i]) isnotNoneand str2ch.get(ss[i]) != pattern[i]:
12+
if vinstr2cand str2c[v] != k:
1213
return False
13-
ch2str[pattern[i]] = ss[i]
14-
str2ch[ss[i]] = pattern[i]
14+
c2str[k], str2c[v] = v, k
1515
return True

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /