forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimilar String Groups.js
40 lines (36 loc) · 963 Bytes
/
Similar String Groups.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var numSimilarGroups = function(strs) {
const len = strs.length;
// dsu logic
const dsu = new Array(len).fill(0).map((_, idx) => idx);
const find = (x) => {
if(x == dsu[x]) return x;
return dsu[x] = find(dsu[x]);
}
const union = (i, j) => {
const pi = find(i);
const pj = find(j);
if(pi != pj) {
dsu[pi] = pj;
}
}
const similar = (a, b) => {
let c = 0, i = 0, len = a.length;
while(i < len) {
if(a[i] != b[i]) c++;
i++;
if(c > 2) return false;
}
return true;
}
for(let i = 0; i < len; i++) {
for(let j = i + 1; j < len; j++) {
if(similar(strs[i], strs[j])) {
union(i, j);
}
}
}
for(let i = 0; i < len; i++) find(i);
const s = new Set();
for(let i = 0; i < len; i++) s.add(dsu[i]);
return s.size;
};