Skip to content

Commit 63af104

Browse files
committed
feat: add ts solution to lc problem: No.1374
No.0990.Satisfiability of Equality Equations
1 parent d8e861c commit 63af104

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

solution/0900-0999/0990.Satisfiability of Equality Equations/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,51 @@ func equationsPossible(equations []string) bool {
248248
}
249249
```
250250

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+
251296
### **...**
252297

253298
```

solution/0900-0999/0990.Satisfiability of Equality Equations/README_EN.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,51 @@ func equationsPossible(equations []string) bool {
162162
}
163163
```
164164

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+
165210
### **...**
166211

167212
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

0 commit comments

Comments
 (0)