-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem1061.cpp
43 lines (39 loc) · 1.29 KB
/
problem1061.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
// disjoint sets union
struct dsu{
vector<int> parent;
dsu(){
parent = vector<int>(26);
for(int i = 0;i < 26;i++)
parent[i] = i;
}
// to find parent(smallest char in set)
int getParent(int x){
if(x == parent[x])
return x;
return parent[x] = getParent(parent[x]);
}
// to uni between two sets
void uni(int x, int y){
x = getParent(x);
y = getParent(y);
// if x, y has same parent(same set)
if(x == y)
return;
// Choose the smallest parent of the set to be the parent of another set
if(x < y)
parent[y] = parent[x];
else
parent[x] = parent[y];
}
};
class Solution {
public:
string smallestEquivalentString(string s1, string s2, string baseStr) {
dsu d;
for(int i = 0;i < s1.size();i++)
d.uni((int)(s1[i] - 'a'), (int)(s2[i] - 'a'));
for(auto &k : baseStr)
k = (char)(d.getParent(k-'a')+'a');
return baseStr;
}
};