intfindFather(int x){ return x == f[x] ? x : f[x] = findFather(f[x]); }
boolcheck(string &a, string &b, int len){ int cnt = 0; for (int i = 0; i < len; i++) { if (a[i] != b[i]) { cnt++; if (cnt > 2) return0; } } return1; }
intnumSimilarGroups(vector<string> &strs){ int n = strs.size(); if (n == 0) return0; f.resize(n); for (int i = 0; i < n; i++) f[i] = i; int len = strs[0].size(), ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int fi = findFather(i), fj = findFather(j); if (fi == fj) continue; if (check(strs[i], strs[j], len)) { f[fi] = fj; } } } for (int i = 0; i < n; i++) if (f[i] == i) ans++; return ans; } };