forked from Chayandas07/LeetCode-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1061. Lexicographically Smallest Equivalent String
More file actions
33 lines (33 loc) · 1.13 KB
/
1061. Lexicographically Smallest Equivalent String
File metadata and controls
33 lines (33 loc) · 1.13 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
class Solution {
public:
char dfs(char c, vector<char>& unionGraph) {
if(unionGraph[c] != '\0') return dfs(unionGraph[c], unionGraph);
return c;
}
void merge(char big, char small, vector<char>& unionGraph) {
if(big=='\0' || small == '\0') return;
if(unionGraph[big] > small) {
// cout<<unionGraph[big]<<" "<<small<<endl;
merge(unionGraph[big], small, unionGraph);
} else if(unionGraph[big] < small) {
char temp = unionGraph[small];
char temp1 = unionGraph[big];
unionGraph[big] = small;
unionGraph[small] = temp1;
merge(small, temp, unionGraph);
}
}
string smallestEquivalentString(string s1, string s2, string baseStr) {
vector<char> unionGraph(200, '\0');
for(int i = 0; i < s1.size(); i ++) {
char big = max(s1[i], s2[i]);
char small = min(s1[i], s2[i]);
if(big != small) merge(big, small, unionGraph);
}
string res = "";
for(auto &ch: baseStr) {
res += dfs(ch, unionGraph);
}
return res;
}
};