File tree Expand file tree Collapse file tree 3 files changed +130
-0
lines changed
solution/0900-0999/0990.Satisfiability of Equality Equations Expand file tree Collapse file tree 3 files changed +130
-0
lines changed Original file line number Diff line number Diff line change @@ -248,6 +248,51 @@ func equationsPossible(equations []string) bool {
248
248
}
249
249
```
250
250
251
+ ### ** TypeScript**
252
+
253
+ ``` ts
254
+ class UnionFind {
255
+ private parent: number [];
256
+
257
+ constructor () {
258
+ this .parent = Array .from ({ length: 26 }).map ((_ , i ) => i );
259
+ }
260
+
261
+ find(index : number ) {
262
+ if (this .parent [index ] === index ) {
263
+ return index ;
264
+ }
265
+ this .parent [index ] = this .find (this .parent [index ]);
266
+ return this .parent [index ];
267
+ }
268
+
269
+ union(index1 : number , index2 : number ) {
270
+ this .parent [this .find (index1 )] = this .find (index2 );
271
+ }
272
+ }
273
+
274
+ function equationsPossible(equations : string []): boolean {
275
+ const uf = new UnionFind ();
276
+ for (const [a, s, _, b] of equations ) {
277
+ if (s === ' =' ) {
278
+ const index1 = a .charCodeAt (0 ) - ' a' .charCodeAt (0 );
279
+ const index2 = b .charCodeAt (0 ) - ' a' .charCodeAt (0 );
280
+ uf .union (index1 , index2 );
281
+ }
282
+ }
283
+ for (const [a, s, _, b] of equations ) {
284
+ if (s === ' !' ) {
285
+ const index1 = a .charCodeAt (0 ) - ' a' .charCodeAt (0 );
286
+ const index2 = b .charCodeAt (0 ) - ' a' .charCodeAt (0 );
287
+ if (uf .find (index1 ) === uf .find (index2 )) {
288
+ return false ;
289
+ }
290
+ }
291
+ }
292
+ return true ;
293
+ }
294
+ ```
295
+
251
296
### ** ...**
252
297
253
298
```
Original file line number Diff line number Diff line change @@ -162,6 +162,51 @@ func equationsPossible(equations []string) bool {
162
162
}
163
163
```
164
164
165
+ ### ** TypeScript**
166
+
167
+ ``` ts
168
+ class UnionFind {
169
+ private parent: number [];
170
+
171
+ constructor () {
172
+ this .parent = Array .from ({ length: 26 }).map ((_ , i ) => i );
173
+ }
174
+
175
+ find(index : number ) {
176
+ if (this .parent [index ] === index ) {
177
+ return index ;
178
+ }
179
+ this .parent [index ] = this .find (this .parent [index ]);
180
+ return this .parent [index ];
181
+ }
182
+
183
+ union(index1 : number , index2 : number ) {
184
+ this .parent [this .find (index1 )] = this .find (index2 );
185
+ }
186
+ }
187
+
188
+ function equationsPossible(equations : string []): boolean {
189
+ const uf = new UnionFind ();
190
+ for (const [a, s, _, b] of equations ) {
191
+ if (s === ' =' ) {
192
+ const index1 = a .charCodeAt (0 ) - ' a' .charCodeAt (0 );
193
+ const index2 = b .charCodeAt (0 ) - ' a' .charCodeAt (0 );
194
+ uf .union (index1 , index2 );
195
+ }
196
+ }
197
+ for (const [a, s, _, b] of equations ) {
198
+ if (s === ' !' ) {
199
+ const index1 = a .charCodeAt (0 ) - ' a' .charCodeAt (0 );
200
+ const index2 = b .charCodeAt (0 ) - ' a' .charCodeAt (0 );
201
+ if (uf .find (index1 ) === uf .find (index2 )) {
202
+ return false ;
203
+ }
204
+ }
205
+ }
206
+ return true ;
207
+ }
208
+ ```
209
+
165
210
### ** ...**
166
211
167
212
```
Original file line number Diff line number Diff line change
1
+ class UnionFind {
2
+ private parent : number [ ] ;
3
+
4
+ constructor ( ) {
5
+ this . parent = Array . from ( { length : 26 } ) . map ( ( _ , i ) => i ) ;
6
+ }
7
+
8
+ find ( index : number ) {
9
+ if ( this . parent [ index ] === index ) {
10
+ return index ;
11
+ }
12
+ this . parent [ index ] = this . find ( this . parent [ index ] ) ;
13
+ return this . parent [ index ] ;
14
+ }
15
+
16
+ union ( index1 : number , index2 : number ) {
17
+ this . parent [ this . find ( index1 ) ] = this . find ( index2 ) ;
18
+ }
19
+ }
20
+
21
+ function equationsPossible ( equations : string [ ] ) : boolean {
22
+ const uf = new UnionFind ( ) ;
23
+ for ( const [ a , s , _ , b ] of equations ) {
24
+ if ( s === '=' ) {
25
+ const index1 = a . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ;
26
+ const index2 = b . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ;
27
+ uf . union ( index1 , index2 ) ;
28
+ }
29
+ }
30
+ for ( const [ a , s , _ , b ] of equations ) {
31
+ if ( s === '!' ) {
32
+ const index1 = a . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ;
33
+ const index2 = b . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ;
34
+ if ( uf . find ( index1 ) === uf . find ( index2 ) ) {
35
+ return false ;
36
+ }
37
+ }
38
+ }
39
+ return true ;
40
+ }
You can’t perform that action at this time.
0 commit comments