2
var numSimilarGroups = function (strs) {6
// Compare each string with all other string7
for (let i = 0; i < n; ++i) {8
for (let j = i + 1; j < n; ++j) {9
if (isSimilar(strs[i], strs[j])) uf.union(i, j);13
// uf.count gives number of non connected components in UnionFind data structure16
// UnionFind Data Structure with Strongly connected components19
this.parent = Array(n)22
this.count = n; // Keep track of number of groups25
if (this.parent[i] !== i) this.parent[i] = this.find(this.parent[i]);26
return this.parent[i];29
const x = this.find(i),31
if (x !== y) (this.parent[y] = x), this.count--;35
// Function to check if 2 strings are different at less than 2 positions or not36
var isSimilar = function (str1, str2) {37
if (str1 === str2) return true;39
for (let i = 0; i < str1.length; ++i) {40
if (str1[i] !== str2[i] && ++count > 2) return false;