-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimilarStringGroups.cpp
More file actions
64 lines (56 loc) · 1.38 KB
/
similarStringGroups.cpp
File metadata and controls
64 lines (56 loc) · 1.38 KB
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
// Source: https://leetcode.com/problems/similar-string-groups/
// Author: Miao Zhang
// Date: 2021-03-17
class UnionFind {
public:
UnionFind(int size) : size_(size), root_(size), rank_(size) {
iota(begin(root_), end(root_), 0);
}
int Find(int n) {
if (root_[n] != n) {
root_[n] = Find(root_[n]);
}
return root_[n];
}
void Union(int x, int y) {
int px = Find(x);
int py = Find(y);
if (px == py) return;
if (rank_[py] > rank_[px]) {
swap(px, py);
} else if (rank_[px] == rank_[py]) {
rank_[px]++;
}
root_[py] = px;
size_--;
}
int Size() {
return size_;
}
private:
int size_;
vector<int> root_;
vector<int> rank_;
};
class Solution {
public:
int numSimilarGroups(vector<string>& strs) {
UnionFind uf(strs.size());
for (int i = 0; i < strs.size(); i++) {
for (int j = i + 1; j < strs.size(); j++) {
if (similar(strs[i], strs[j])) {
uf.Union(i, j);
}
}
}
return uf.Size();
}
private:
bool similar(const string& a, const string& b) {
int diff = 0;
for (int i = 0; i < a.length(); i++) {
if (a[i] != b[i] && ++diff > 2) return false;
}
return true;
}
};