Skip to content

Commit

Permalink
Time: 0 ms (100.00%), Space: 6.4 MB (78.90%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Jan 14, 2023
1 parent a964513 commit 5e25579
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
string smallestEquivalentString(string s1, string s2, string baseStr) {
char map[26];
for(int i=0;i<26;i++){
map[i] = i+'a';
}
for(int i=0;i<s1.size();i++){
char replace = max(map[s1[i] - 'a'],map[s2[i] - 'a']); //Bigger element
char put = min(map[s1[i] - 'a'],map[s2[i] - 'a']); // Smaller element
for(int i=0;i<26;i++){
if(map[i] == replace)
map[i] = put; // Replace all the bigger characters with it's smallest equivalent
}
}

string ans = "";
for(int i=0;i<baseStr.size();i++){
ans += map[baseStr[i] - 'a'];
}
return ans;
}
};

0 comments on commit 5e25579

Please sign in to comment.