-
Notifications
You must be signed in to change notification settings - Fork 3
/
839.cpp
72 lines (72 loc) · 1.9 KB
/
839.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class DisjointSet {
private:
vector<int> parent;
vector<int> rank;
int n;
public:
DisjointSet(int size) {
n = size;
parent.resize(size, 0);
rank.resize(size, 0);
for (int i = 0; i < n; ++i) parent[i] = i;
}
int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
void join(int x, int y) {
int pX = find(x);
int pY = find(y);
if (pX != pY) {
n--;
if (rank[pX] > rank[pY]) {
parent[pY] = parent[pX];
}
else if (rank[pX] < rank[pY]) {
parent[pX] = parent[pY];
}
else {
parent[pY] = parent[pX];
rank[pX]++;
}
}
}
int size() {
return n;
}
};
class Solution {
public:
bool isSimilar(const string& s1, const string& s2) {
int n = s1.size();
int m = s2.size();
if (n != m) return false;
int diffCount = 0;
int diffOne = -1, diffTwo = -1;
for (int i = 0; i < n; ++i) {
if (s1[i] != s2[i]) {
diffCount++;
if (diffCount == 1) diffOne = i;
else if (diffCount == 2) diffTwo = i;
else return false;
}
}
if (diffCount <= 1) return true;
if (s1[diffOne] != s2[diffTwo]) return false;
if (s1[diffTwo] != s2[diffOne]) return false;
return true;
}
int numSimilarGroups(vector<string>& strs) {
int n = strs.size();
DisjointSet* disjointSet = new DisjointSet(n);
vector<pair<int, int>> edges;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (isSimilar(strs[i], strs[j])) disjointSet->join(i, j);
}
}
return disjointSet->size();
}
};