File tree 3 files changed +146
-0
lines changed
solution/0200-0299/0290.Word Pattern
3 files changed +146
-0
lines changed Original file line number Diff line number Diff line change @@ -124,6 +124,32 @@ function wordPattern(pattern: string, s: string): boolean {
124
124
}
125
125
```
126
126
127
+ ``` ts
128
+ function wordPattern(pattern : string , s : string ): boolean {
129
+ const n = pattern .length ;
130
+ const cs = s .split (' ' );
131
+ if (n !== cs .length ) {
132
+ return false ;
133
+ }
134
+ const map1 = new Map <string , number >();
135
+ const map2 = new Map <string , number >();
136
+ for (let i = 0 ; i < n ; i ++ ) {
137
+ const c1 = pattern [i ];
138
+ const c2 = cs [i ];
139
+ if (! map1 .has (c1 )) {
140
+ map1 .set (c1 , i );
141
+ }
142
+ if (! map2 .has (c2 )) {
143
+ map2 .set (c2 , i );
144
+ }
145
+ if (map1 .get (c1 ) !== map2 .get (c2 )) {
146
+ return false ;
147
+ }
148
+ }
149
+ return true ;
150
+ }
151
+ ```
152
+
127
153
### ** C++**
128
154
129
155
``` cpp
@@ -177,6 +203,39 @@ func wordPattern(pattern string, s string) bool {
177
203
}
178
204
```
179
205
206
+ ### ** Rust**
207
+
208
+ ``` rust
209
+ use std :: collections :: HashMap ;
210
+
211
+ impl Solution {
212
+ pub fn word_pattern (pattern : String , s : String ) -> bool {
213
+ let cs1 : Vec <char > = pattern . chars (). collect ();
214
+ let cs2 : Vec <& str > = s . split_whitespace (). collect ();
215
+ let n = cs1 . len ();
216
+ if n != cs2 . len () {
217
+ return false ;
218
+ }
219
+ let mut map1 = HashMap :: new ();
220
+ let mut map2 = HashMap :: new ();
221
+ for i in 0 .. n {
222
+ let c = cs1 [i ];
223
+ let s = cs2 [i ];
224
+ if ! map1 . contains_key (& c ) {
225
+ map1 . insert (c , i );
226
+ }
227
+ if ! map2 . contains_key (& s ) {
228
+ map2 . insert (s , i );
229
+ }
230
+ if map1 . get (& c ) != map2 . get (& s ) {
231
+ return false
232
+ }
233
+ }
234
+ true
235
+ }
236
+ }
237
+ ```
238
+
180
239
### ** ...**
181
240
182
241
```
Original file line number Diff line number Diff line change @@ -117,6 +117,32 @@ function wordPattern(pattern: string, s: string): boolean {
117
117
}
118
118
```
119
119
120
+ ``` ts
121
+ function wordPattern(pattern : string , s : string ): boolean {
122
+ const n = pattern .length ;
123
+ const cs = s .split (' ' );
124
+ if (n !== cs .length ) {
125
+ return false ;
126
+ }
127
+ const map1 = new Map <string , number >();
128
+ const map2 = new Map <string , number >();
129
+ for (let i = 0 ; i < n ; i ++ ) {
130
+ const c1 = pattern [i ];
131
+ const c2 = cs [i ];
132
+ if (! map1 .has (c1 )) {
133
+ map1 .set (c1 , i );
134
+ }
135
+ if (! map2 .has (c2 )) {
136
+ map2 .set (c2 , i );
137
+ }
138
+ if (map1 .get (c1 ) !== map2 .get (c2 )) {
139
+ return false ;
140
+ }
141
+ }
142
+ return true ;
143
+ }
144
+ ```
145
+
120
146
### ** C++**
121
147
122
148
``` cpp
@@ -170,6 +196,39 @@ func wordPattern(pattern string, s string) bool {
170
196
}
171
197
```
172
198
199
+ ### ** Rust**
200
+
201
+ ``` rust
202
+ use std :: collections :: HashMap ;
203
+
204
+ impl Solution {
205
+ pub fn word_pattern (pattern : String , s : String ) -> bool {
206
+ let cs1 : Vec <char > = pattern . chars (). collect ();
207
+ let cs2 : Vec <& str > = s . split_whitespace (). collect ();
208
+ let n = cs1 . len ();
209
+ if n != cs2 . len () {
210
+ return false ;
211
+ }
212
+ let mut map1 = HashMap :: new ();
213
+ let mut map2 = HashMap :: new ();
214
+ for i in 0 .. n {
215
+ let c = cs1 [i ];
216
+ let s = cs2 [i ];
217
+ if ! map1 . contains_key (& c ) {
218
+ map1 . insert (c , i );
219
+ }
220
+ if ! map2 . contains_key (& s ) {
221
+ map2 . insert (s , i );
222
+ }
223
+ if map1 . get (& c ) != map2 . get (& s ) {
224
+ return false
225
+ }
226
+ }
227
+ true
228
+ }
229
+ }
230
+ ```
231
+
173
232
### ** ...**
174
233
175
234
```
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashMap ;
2
+
3
+ impl Solution {
4
+ pub fn word_pattern ( pattern : String , s : String ) -> bool {
5
+ let cs1: Vec < char > = pattern. chars ( ) . collect ( ) ;
6
+ let cs2: Vec < & str > = s. split_whitespace ( ) . collect ( ) ;
7
+ let n = cs1. len ( ) ;
8
+ if n != cs2. len ( ) {
9
+ return false ;
10
+ }
11
+ let mut map1 = HashMap :: new ( ) ;
12
+ let mut map2 = HashMap :: new ( ) ;
13
+ for i in 0 ..n {
14
+ let c = cs1[ i] ;
15
+ let s = cs2[ i] ;
16
+ if !map1. contains_key ( & c) {
17
+ map1. insert ( c, i) ;
18
+ }
19
+ if !map2. contains_key ( & s) {
20
+ map2. insert ( s, i) ;
21
+ }
22
+ if map1. get ( & c) != map2. get ( & s) {
23
+ return false
24
+ }
25
+ }
26
+ true
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments