🔥 C++ || Step by step example || O(n) time, O(1) space 🔥
Before looking at the example, look at the solution. It is only a step by step illustration.\n\n# Example\ns1 = "acde", s2 = "bcea", baseStr = "bdeac"\n\n### In
**Before looking at the example, look at the solution. It is only a step by step illustration.**\n\n# Example\n`s1 = "acde"`, `s2 = "bcea"`, `baseStr = "bdeac"`\n\n### Initialization\n| a | b | c | d | e |\n|:-:|:-:|:-:|:-:|:-:|\n| a | b | c | d | e |\n\n### Step 0:\n> s1 = "**a**cde", s2 = "**b**cea"\n\n`s1[0] = a, s2[0] = b, table[s1[0]] = a, table[s2[0]] = b`\n`curr_min = a, curr_max = b \u21D2 table[b] = a`\n| **a** | **b** | c | d | e |\n|:-----:|:-----:|:-:|:-:|:-:|\n| **a** | **a** | c | d | e |\n\n### Step 1:\n> s1 = "a**c**de", s2 = "b**c**ea"\n\n`s1[1] = \u0441, s2[1] = c, table[s1[1]] = c, table[s2[1]] = c`\n`curr_min = c, curr_max = c \u21D2 table[c] = c`\n| a | b | **c** | d | e |\n|:-:|:-:|:-----:|:-:|:-:|\n| a | b | **c** | d | e |\n\n### Step 2:\n> s1 = "ac**d**e", s2 = "bc**e**a"\n\n`s1[2] = d, s2[2] = e, table[s1[2]] = d, table[s2[2]] = e`\n`curr_min = d, curr_max = e \u21D2 table[e] = d`\n| a | b | c | **d** | **e** |\n|:-:|:-:|:-:|:-----:|:-----:|\n| a | a | c | **d** | **d** |\n\n### Step 3:\n> s1 = "acd**e**", s2 = "bce**a**"\n\n`s1[3] = e, s2[3] = a, table[s1[3]] = d, table[s2[3]] = a`\n`curr_min = a, curr_max = d \u21D2 table[d] = d, table[e] = d`\n(Since we are looking for all cells whose value is `curr_max`)\n| **a** | **b** | c | **d** | **e** |\n|:-----:|:-----:|:-:|:-----:|:-----:|\n| **a** | **a** | c | **a** | **a** |\n\n### Formation of the result\n> baseStr = "bdeac"\n\n`b \u21D2 a, d \u21D2 a, e \u21D2 a, a \u21D2 a, c \u21D2 c `\n\nLexicographically smallest equivalent string of `baseStr`: `"aaaac"`\n\n# Complexity\n- Time complexity: $$ O(n \\cdot 26) = O(n) $$\n- Space complexity: $$ O(26) = O(1) $$\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    string smallestEquivalentString(string s1, string s2, string baseStr) {\n        vector<int> table(26);\n        for (int i = 0; i < 26; i++) { table[i] = \'a\' + i; }\n        for (int i = 0; i < s1.size(); i++) {\n            int curr_min = table[s1[i]-\'a\'], curr_max = table[s2[i]-\'a\'];\n            if (curr_max < curr_min) { swap(curr_min, curr_max); }\n            for (int j = 0; j < 26; j++) {\n                if (table[j] == curr_max) { table[j] = curr_min; }\n            }\n        }\n        for (int i = 0; i < baseStr.size(); i++) { baseStr[i] = table[baseStr[i]-\'a\']; }\n        return baseStr;\n    }\n};\n```
['C++']