id
int64 1
3.58k
| problem_description
stringlengths 516
21.8k
| instruction
int64 0
3
| solution_c
dict |
---|---|---|---|
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int par[26];\n \n int find(int x){\n if(par[x]==-1) return x;\n return par[x]=find(par[x]);\n }\n \n void Union(int x, int y) {\n x = find(x);\n y = find(y);\n \n if (x != y) \n par[max(x, y)] = min(x, y); \n }\n\t\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n \n memset(par, -1, sizeof(par));\n \n for (auto i = 0; i < s1.size(); ++i) \n Union(s1[i] - 'a', s2[i] - 'a');\n \n for(auto i=0;i<baseStr.size();i++) \n baseStr[i]=find(baseStr[i]-'a')+'a';\n\n return baseStr;\n }\n};",
"memory": "7900"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class UnionFind {\nprivate:\n int root[26] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,\n 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25};\npublic:\n int Find(int node)\n {\n if(root[node] != node)\n {\n root[node] = Find(root[node]);\n }\n return root[node];\n }\n void Join(int n1, int n2)\n {\n int r1 = Find(n1);\n int r2 = Find(n2);\n if(r1 > r2)\n {\n root[r1] = r2;\n }\n else\n {\n root[r2] = r1;\n }\n }\n};\nclass Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr)\n {\n UnionFind graph;\n for(int i = 0; i < s1.size(); i++)\n {\n graph.Join(s1[i] - 'a', s2[i] - 'a');\n }\n for(auto& ch: baseStr)\n {\n ch = 'a' + graph.Find(ch - 'a');\n }\n return baseStr;\n }\n};\n",
"memory": "8000"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n struct UnionFind {\n UnionFind() {\n std::iota(parents.begin(), parents.end(), 0);\n std::iota(chars.begin(), chars.end(), 0);\n }\n \n int find(int value) {\n if (parents[value] == value)\n return value;\n \n parents[value] = find(parents[value]);\n \n return parents[value];\n }\n \n void merge(int f, int s) {\n const auto root1 = find(f);\n const auto root2 = find(s);\n \n if (root1 != root2) {\n parents[root2] = root1;\n\n chars[root1] = std::min(chars[root1], chars[root2]);\n chars[root2] = INT_MAX;\n }\n }\n \n std::array<int, 26> parents;\n std::array<int, 26> chars;\n };\n \npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n std::string result;\n \n UnionFind uf;\n \n for (size_t i = 0; i < s1.size(); ++i)\n uf.merge(s1[i] - 'a', s2[i] - 'a');\n\n for (const auto ch : baseStr) {\n const auto root = uf.find(ch - 'a');\n const auto next_ch = uf.chars[root] + 'a';\n \n result += next_ch;\n }\n \n return result;\n }\n};",
"memory": "8100"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n struct UnionFind {\n UnionFind() {\n std::iota(parents.begin(), parents.end(), 0);\n std::iota(chars.begin(), chars.end(), 0);\n }\n \n int find(int value) {\n if (parents[value] == value)\n return value;\n \n parents[value] = find(parents[value]);\n \n return parents[value];\n }\n \n void merge(int f, int s) {\n const auto root1 = find(f);\n const auto root2 = find(s);\n \n if (root1 != root2) {\n parents[root2] = root1;\n\n chars[root1] = std::min(chars[root1], chars[root2]);\n chars[root2] = INT_MAX;\n }\n }\n \n std::array<int, 26> parents;\n std::array<int, 26> chars;\n };\n \npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n std::string result;\n \n UnionFind uf;\n \n for (size_t i = 0; i < s1.size(); ++i)\n uf.merge(s1[i] - 'a', s2[i] - 'a');\n\n for (const auto ch : baseStr) {\n const auto root = uf.find(ch - 'a');\n const auto next_ch = uf.chars[root] + 'a';\n \n result += next_ch;\n }\n \n return result;\n }\n};",
"memory": "8100"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class DisjointSet\n{\n\tpublic:\n\tvector<int> parent;\n\tvector<int> size;\n\n\tDisjointSet(int n)\n\t{\n\t\tparent.resize(n);\n\t\tsize.resize(n, 1);\n\n\t\tfor(int i=0; i<n; i++)\n\t\tparent[i] = i;\n\t}\n\n\tint findParent(int node)\n\t{\n\t\tif(node == parent[node])\n\t\treturn node;\n\n\t\telse\n\t\treturn parent[node] = findParent(parent[node]);\n\t}\n\n\tvoid unionByVal(int u, int v)\n\t{\n\t\tint pu = findParent(u);\n\t\tint pv = findParent(v);\n\t\tif(pu != pv)\n\t\t{\n\t\t\tif(pu < pv)\n\t\t\tparent[pv] = pu;\n\n\t\t\telse\n\t\t\tparent[pu] = pv;\n\t\t}\n\t}\n};\n\nclass Solution {\npublic:\n\n string smallestEquivalentString(string s, string t, string str)\n {\n // Write your code here.\n DisjointSet ds(26);\n\n for(int i=0; i<s.length(); i++)\n {\n int u = s[i] - 'a';\n int v = t[i] - 'a';\n ds.unionByVal(u, v);\n }\n\n string ans = \"\";\n for(int i=0; i<str.length(); i++)\n {\n ans += ds.findParent(str[i] - 'a') + 'a';\n }\n return ans;\n }\n};",
"memory": "8200"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\nvector<int> headChar;\n\n//find parent\nint find(int x)\n{\n\tif(headChar[x]==-1)\n\treturn x;\n\treturn headChar[x]=find(headChar[x]);\n}\n\n//union \nvoid Union(int x,int y)\n{\n\tint parentX=find(x);\n\tint parentY=find(y);\n\t\n\tif(parentX==parentY)\n\treturn;\n\n\t//make smaller one represent of another. if parents are different\n\theadChar[max(parentX,parentY)] = min(parentX,parentY);\n}\n \n string smallestEquivalentString(string s1, string s2, string baseStr) {\n \n\theadChar.resize(26,-1);\n\t//make group --Union\n\n\tfor(int i=0;i<s1.size();i++)\n\t{\n\t\tUnion(s1[i]-'a',s2[i]-'a');\n\t}\n \n\tfor(auto i=0;i<baseStr.size();i++)\n\t{\n\t\tbaseStr[i]=find(baseStr[i]-'a')+'a';\n\t}\n\treturn baseStr;\n }\n};",
"memory": "8200"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Disjointset{\npublic:\n vector<int>size,parent;\n Disjointset (int n){\n size.resize(n);\n parent.resize(n);\n for(int i = 0; i < n; i++){\n size[i] = 1;\n parent[i] = i;\n }\n }\n\n int findUparent(int node){\n if(node == parent[node]){\n return node;\n }\n\n return parent[node] = findUparent(parent[node]);\n }\n\n void unionbysize(int u,int v){\n int ulp_u = findUparent(u);\n int ulp_v = findUparent(v);\n\n if(ulp_u == ulp_v){\n return;\n }\n\n else if(size[ulp_v] < size[ulp_u]){\n parent[ulp_v] = ulp_u;\n size[ulp_u] = size[ulp_u] + size[ulp_v];\n }\n\n else if(size[ulp_u] < size[ulp_v]){\n parent[ulp_u] = ulp_v;\n size[ulp_v] = size[ulp_v] + size[ulp_u];\n }\n\n else{\n parent[ulp_v] = ulp_u;\n size[ulp_u] = size[ulp_u] + size[ulp_v];\n }\n\n }\n\n};\n\nclass Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n Disjointset ds(26);\n int n = s1.size();\n for(int i = 0; i < n; i++){\n int fi = int(s1[i] - 97);\n int se = int(s2[i] - 97);\n\n if(ds.findUparent(fi) != ds.findUparent(se)){\n ds.unionbysize(fi,se);\n }\n\n }\n\n string res = \"\";\n for(int i = 0; i < baseStr.size(); i++){\n int t = 27;\n for(int j = 0; j < 26; j++){\n if(ds.findUparent(j) == ds.findUparent(int(baseStr[i] - 97))){\n t = min(t,min(j,int(baseStr[i] - 97)));\n }\n }\n\n res += char(t + 97);\n }\n\n return res;\n }\n};",
"memory": "8300"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n char findParent(int child, vector<int> &parent) {\n if (child == parent[child]) {\n return child;\n }\n\n return parent[child] = findParent(parent[child], parent);\n }\n\n void merge(int c1, int c2, vector<int> &parent) {\n int p1 = findParent(c1, parent);\n int p2 = findParent(c2, parent);\n \n if (p1 < p2) {\n parent[p2] = p1;\n } else {\n parent[p1] = p2;\n }\n }\n\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n vector<int> parent(26, 0);\n\n for (int i = 0; i < 26; i++) {\n parent[i] = i;\n }\n\n for (int i = 0; i < s1.size(); i++) {\n merge(s1[i]-'a', s2[i]-'a', parent);\n }\n string ans = \"\";\n\n for (int i = 0; i < baseStr.size(); i++) {\n char targetParent = findParent(baseStr[i]-'a', parent) + 'a';\n ans.push_back(targetParent);\n }\n\n return ans;\n }\n};",
"memory": "8300"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\n struct UnionFind {\n UnionFind() {\n std::iota(parents.begin(), parents.end(), 0);\n std::iota(chars.begin(), chars.end(), 0);\n }\n \n int find(int value) {\n if (parents[value] == value)\n return value;\n \n parents[value] = find(parents[value]);\n \n return parents[value];\n }\n \n void merge(int f, int s) {\n const auto root1 = find(f);\n const auto root2 = find(s);\n \n if (root1 != root2) {\n parents[root2] = root1;\n\n chars[root1] = std::min(chars[root1], chars[root2]);\n chars[root2] = INT_MAX;\n }\n }\n \n std::array<int, 26> parents;\n std::array<int, 26> chars;\n };\n \npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n std::string result;\n \n UnionFind uf;\n \n for (size_t i = 0; i < s1.size(); ++i)\n uf.merge(s1[i] - 'a', s2[i] - 'a');\n\n for (const auto ch : baseStr) {\n const auto root = uf.find(ch - 'a');\n const auto next_ch = uf.chars[root] + 'a';\n \n result += next_ch;\n }\n \n return result;\n }\n};",
"memory": "8400"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n// Node struct to represent a character and its properties in the Disjoint-Set\nstruct Node{\n int id =-1; // id of the character, initialize it to -1 to represent uninitialized state\n char label=' '; // label of the character, initialize it to ' ' to represent uninitialized state\n Node* parent=this; // pointer to the parent node, initialize it to this to represent the root node\n int size=1; // size of the set, initialize it to 1 as it is the root node\n bool visted=0; // boolean to check if the node is visited, not used in this code\n vector<Node*> neighbours={}; // neighbours of the node, not used in this code\n Node(int _id, char _label):id(_id), label(_label){} // constructor to initialize the id and label of the node\n};\n\n// function to find the parent of a node in the Disjoint-Set\nNode* getParentDSU(Node* node){\n if(node==node->parent) return node; // if the node is the root node, return it\n return node->parent=getParentDSU(node->parent); // else recursively find the parent and use path compression to update the parent\n}\n\n// function to join two nodes in the Disjoint-Set\nvoid joinDSU(Node* src,Node* tar){\n src=getParentDSU(src); // find the parent of the source node\n tar=getParentDSU(tar); // find the parent of the target node\n if(src!=tar){ // if the two parents are different\n if(tar->label<src->label) swap(src,tar); // ensure the label of the parent is lexicographically smaller\n tar->parent=src; // set the parent of target to source\n src->size+=tar->size; // increment the size of the set\n }\n}\n\nstring smallestEquivalentString(string s1, string s2, string baseStr) {\n // Create an array of nodes representing each character\n vector<Node*> nodes(26);\n for(int i=0;i<26;i++){\n nodes[i] = new Node(i+1,'a'+i); // initialize the nodes with id and label\n }\n\n // For each pair of characters in s1 and s2, join their parents in the Disjoint-Set\n for(int i=0;i<s1.size();i++){\n joinDSU(nodes[s1[i]-'a'],nodes[s2[i]-'a']);\n }\n\n // For each character in baseStr, replace it with its equivalent parent in the Disjoint-Set\n for(int i=0;i<baseStr.size();i++){\n if(getParentDSU(nodes[baseStr[i]-'a'])->label<baseStr[i]){\n baseStr[i]=getParentDSU(nodes[baseStr[i]-'a'])->label;\n }\n \n}\n\n return baseStr;\n}\n};",
"memory": "8500"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n class DisjointSet {\n private: \n vector<int> parent;\n public:\n DisjointSet(int n) : parent(n, 1) {\n for (int i = 0; i < n; ++i) {\n parent[i] = i;\n }\n }\n\n int FindRoot(int p) {\n int root = p;\n while (root != parent[root]) {\n root = parent[root];\n }\n // path compression\n while (root != p) {\n int newP = parent[p];\n parent[p] = root;\n p = newP;\n }\n\n return root;\n }\n\n void Union(int p, int q) {\n int rootP = FindRoot(p);\n int rootQ = FindRoot(q);\n\n if (rootP == rootQ) {\n return;\n }\n\n if (rootP < rootQ) {\n parent[rootQ] = rootP;\n } else {\n parent[rootP] = rootQ;\n }\n }\n\n };\n\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n = s1.length();\n DisjointSet ds(26);\n\n for (int i = 0; i < n; ++i) {\n int p = s1[i] - 'a', q = s2[i] - 'a';\n ds.Union(p, q);\n }\n\n stringstream ans;\n for (char b : baseStr) {\n int root = ds.FindRoot(b - 'a');\n ans << char('a' + root);\n }\n\n return ans.str();\n }\n};",
"memory": "8600"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void dfs(vector<char> adj[], char node, char minCharacter, vector<char>& visited)\n {\n // to this character, replace it with it's minimum character\n visited[node - 'a'] = minCharacter;\n \n // travel in neighbour of this character,\n // i.e travel in component of this character\n for(char& near: adj[node - 'a'])\n {\n // if this character is not visited, call dfs for it\n if(visited[near - 'a'] == '#')\n {\n dfs(adj, near, minCharacter, visited);\n }\n }\n }\n \n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n = s1.size(); // extract size\n \n vector<char> adj[26]; // declaring adjacency matrix\n \n // travel through strings and establish connection\n for(int i = 0; i < n; i++)\n {\n adj[s1[i] - 'a'].push_back(s2[i]);\n adj[s2[i] - 'a'].push_back(s1[i]);\n }\n \n // make a visited vector of size 26\n // and symbol, '#' denotes that this particular character is not visited yet\n vector<char> visited(26, '#');\n \n // now, travel through each character\n for(char c = 'a'; c <= 'z'; c++)\n {\n // if this character is not visited yet, call dfs here \n if(visited[c - 'a'] == '#')\n {\n // as we are calling dfs for each character, so\n // it will minimum value for that component.\n dfs(adj, c, c, visited);\n }\n }\n \n // Now lastly, replace each baseStr character with their \n // minimum found out character in their component,\n for(int i = 0; i < baseStr.size(); i++)\n {\n baseStr[i] = visited[baseStr[i] - 'a'];\n }\n \n // return baseStr now, \n return baseStr;\n }\n};",
"memory": "8700"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n\nvoid dfs(int node ,vector<int>&vis,vector<int>adj[],int &val,vector<int>&correspondence){\n\n vis[node]=1;\n val = min(val,node);\n for(auto &child : adj[node]){\n if(vis[child]) continue;\n\n dfs(child,vis,adj,val,correspondence);\n \n }\n correspondence[node] = val;\n}\n\nstring smallestEquivalentString(string s1, string s2, string baseStr) {\n vector<int>adj[26];\n int n = s1.size();\n for(int i=0;i<n;i++){\n adj[s1[i]-'a'].push_back(s2[i]-'a');\n adj[s2[i]-'a'].push_back(s1[i]-'a');\n }\n vector<int>vis(26,0);\n vector<int>correspondence(26,0);\n\n for(int i=0;i<26;i++){\n if(!vis[i]){\n int val = 27;\n dfs(i,vis,adj,val,correspondence);\n }\n }\n\n string ans=\"\";\n for(auto &it : baseStr){\n ans.push_back(correspondence[it-'a']+'a');\n }\n\n return ans;\n \n}\n\n\n};",
"memory": "8800"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n \n std::vector<std::vector<int>> adjList(26);\n for (int i = 0; i < s1.size(); ++i){\n adjList[s1[i] - 'a'].emplace_back(s2[i] - 'a');\n adjList[s2[i] - 'a'].emplace_back(s1[i] - 'a');\n }\n\n std::vector<int> parent(26, -1);\n for (int i = 0; i < parent.size(); ++i){\n if (parent[i] == -1){\n parent[i] = i;\n dfs(i, parent, adjList);\n }\n }\n \n std::string result;\n for (const auto &c : baseStr){\n result += 'a' + parent[c - 'a'];\n }\n\n return result;\n }\nprivate:\n void dfs(int n, std::vector<int> &parent, const std::vector<std::vector<int>> &adjList){\n for (const auto &nb : adjList[n]){\n if (parent[nb] == -1){\n parent[nb] = parent[n];\n dfs(nb, parent, adjList);\n }\n }\n }\n};",
"memory": "8800"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n \n std::vector<std::vector<int>> adjList(26);\n for (int i = 0; i < s1.size(); ++i){\n adjList[s1[i] - 'a'].emplace_back(s2[i] - 'a');\n adjList[s2[i] - 'a'].emplace_back(s1[i] - 'a');\n }\n\n std::vector<int> parent(26, -1);\n for (int i = 0; i < parent.size(); ++i){\n if (parent[i] == -1){\n parent[i] = i;\n dfs(i, parent, adjList);\n }\n }\n \n std::string result;\n for (const auto &c : baseStr){\n result += 'a' + parent[c - 'a'];\n }\n\n return result;\n }\nprivate:\n void dfs(int n, std::vector<int> &parent, const std::vector<std::vector<int>> &adjList){\n for (const auto &nb : adjList[n]){\n if (parent[nb] == -1){\n parent[nb] = parent[n];\n dfs(nb, parent, adjList);\n }\n }\n }\n};",
"memory": "8900"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\n private: \n void dfs(int par, int node , int minarr[] , int vis[] , vector<vector<int>> &adj)\n {\n vis[node] = 1;\n \n for(auto it : adj[node])\n {\n if(!vis[it] && it!=par && it!=node){\n dfs(node,it,minarr,vis,adj);\n \n minarr[node] = min(minarr[node] , minarr[it]);\n }\n }\n \n \n }\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n vector<vector<int>> adj(26);\n for(int i = 0 ; i < s1.length() ; i++){\n adj[s1[i] - 'a'].push_back(s2[i] - 'a');\n adj[s2[i] - 'a'].push_back(s1[i] - 'a');\n }\n int minarr[26];\n for(int i =0 ; i < 26 ; i++){\n minarr[i]=i;\n }\n for(auto it : baseStr)\n {\n int vis[26] = {0};\n // int m=0;\n dfs(-1,it-'a', minarr, vis, adj);\n }\n string strs= \"\";\n for(auto it :baseStr)\n {\n strs+=char(minarr[it -'a'] + 'a');\n }\n return strs;\n }\n};",
"memory": "9000"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "struct DisjointSetUnion {\n DisjointSetUnion(int _size): size(_size) {\n for (int i = 0; i < _size; ++i)\n parent.emplace_back(i);\n };\n void unionSets(int a, int b){\n a = getParent(a);\n b = getParent(b);\n if (a != b){\n if (size[a] < size[b])\n std::swap(a, b);\n parent[b] = a;\n size[a] += size[b];\n }\n }\n std::vector<int> updateAndOutput(){\n for (int i = 0; i < parent.size(); ++i)\n getParent(i);\n return parent;\n }\nprivate:\n std::vector<int> parent, size;\n int getParent(const int &s){\n if (s == parent[s])\n return s;\n return parent[s] = getParent(parent[s]);\n }\n};\n\nclass Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n \n DisjointSetUnion dsu(26);\n for (int i = 0; i < s1.size(); ++i){\n dsu.unionSets(s1[i] - 'a', s2[i] - 'a');\n }\n\n std::vector<int> parent = dsu.updateAndOutput();\n std::vector<char> tran(26);\n for (int i = 0; i < 26; ++i){\n if (tran[i] == 0){\n if (tran[parent[i]] == 0 && i <= parent[i])\n tran[parent[i]] = 'a' + i;\n tran[i] = tran[parent[i]];\n }\n }\n\n std::string result;\n for (const auto &c : baseStr){\n result += tran[c - 'a'];\n }\n\n return result;\n }\n};",
"memory": "9100"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "struct DisjointSetUnion {\n DisjointSetUnion(int _size): size(_size) {\n for (int i = 0; i < _size; ++i)\n parent.emplace_back(i);\n };\n void unionSets(int a, int b){\n a = getParent(a);\n b = getParent(b);\n if (a != b){\n if (size[a] < size[b])\n std::swap(a, b);\n parent[b] = a;\n size[a] += size[b];\n }\n }\n std::vector<int> updateAndOutput(){\n for (int i = 0; i < parent.size(); ++i)\n getParent(i);\n return parent;\n }\nprivate:\n std::vector<int> parent, size;\n int getParent(const int &s){\n if (s == parent[s])\n return s;\n return parent[s] = getParent(parent[s]);\n }\n};\n\nclass Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n \n DisjointSetUnion dsu(26);\n for (int i = 0; i < s1.size(); ++i){\n dsu.unionSets(s1[i] - 'a', s2[i] - 'a');\n }\n\n std::vector<int> parent = dsu.updateAndOutput();\n std::vector<char> tran(26);\n for (int i = 0; i < 26; ++i){\n if (tran[i] == 0){\n if (tran[parent[i]] == 0 && i <= parent[i])\n tran[parent[i]] = 'a' + i;\n tran[i] = tran[parent[i]];\n }\n }\n\n std::string result;\n for (const auto &c : baseStr){\n result += tran[c - 'a'];\n }\n\n return result;\n }\n};",
"memory": "9100"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "struct DisjointSetUnion {\n DisjointSetUnion(int _size): size(_size) {\n for (int i = 0; i < _size; ++i)\n parent.emplace_back(i);\n };\n void unionSets(int a, int b){\n a = getParent(a);\n b = getParent(b);\n if (a != b){\n if (size[a] < size[b])\n std::swap(a, b);\n parent[b] = a;\n size[a] += size[b];\n }\n }\n std::vector<int> updateAndOutput(){\n for (int i = 0; i < parent.size(); ++i)\n getParent(i);\n return parent;\n }\nprivate:\n std::vector<int> parent, size;\n int getParent(const int &s){\n if (s == parent[s])\n return s;\n return parent[s] = getParent(parent[s]);\n }\n};\n\nclass Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n \n DisjointSetUnion dsu(26);\n for (int i = 0; i < s1.size(); ++i){\n dsu.unionSets(s1[i] - 'a', s2[i] - 'a');\n }\n\n std::vector<int> parent = dsu.updateAndOutput();\n std::vector<char> tran(26);\n for (int i = 0; i < 26; ++i){\n if (tran[i] == 0){\n if (tran[parent[i]] == 0 && i <= parent[i])\n tran[parent[i]] = 'a' + i;\n tran[i] = tran[parent[i]];\n }\n }\n\n std::string result;\n for (const auto &c : baseStr){\n result += tran[c - 'a'];\n }\n\n return result;\n }\n};",
"memory": "9200"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\n \n struct unionNode {\n \n int parent ; \n int rank = 1 ;\n };\n \n struct unionFind {\n \n unionNode charNodes[26];\n \n int returnParent ( const int & a ) {\n int x = charNodes[a].parent;\n if ( x != a ){\n charNodes[a].parent = returnParent ( x ) ;\n }\n return charNodes[a].parent;\n }\n \n void join ( const int & a , const int & b ) {\n int x = returnParent ( a ) ; int y = returnParent ( b );\n if ( x != y ){\n if ( charNodes[x].rank == charNodes[y].rank ){\n charNodes[y].parent = x ; charNodes[x].rank ++ ;\n }\n else {\n if ( charNodes[y].rank > charNodes[x].rank) swap ( x , y);\n charNodes[y].parent = x; charNodes[x].rank = charNodes[y].rank + 1;\n }\n }\n }\n };\n \npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n \n unionFind uF;\n for ( size_t i = 0 ; i < 26 ; ++ i ) uF.charNodes[i].parent = i;\n for ( size_t i = 0 ; i < s1.length() ; ++ i) uF.join ( s1[i]-'a', s2[i]-'a');\n \n int charNodes[26];\n \n for ( size_t i = 0 ; i < 26 ; ++ i) charNodes[i] = INT_MAX ;\n for ( size_t i = 0 ; i < 26 ; ++ i ){\n int parent = uF.returnParent ( i );\n charNodes[parent] = min ( charNodes[parent], (int)i);\n }\n string res = \"\";\n for ( size_t i = 0 ; i < baseStr.length() ; ++ i){\n int parent = uF.returnParent ( baseStr[i] - 'a' ) ;\n res += (charNodes[parent] + 'a');\n }\n \n return res ;\n }\n};",
"memory": "9300"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> parent;\n vector<int> rank;\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n = s1.size();\n parent.resize(26);\n rank.resize(26);\n for(int i = 0;i<26;i++){\n parent[i] = i;\n rank[i] = 0;\n }\n unordered_map<char,char> m;\n for(int i = 0;i<n;i++){\n merge(s1[i]-'a',s2[i]-'a');\n }\n for(int i = 0;i<26;i++){\n if(m.count(find(i)) == 0){\n m[find(i)] = 'a'+i;\n }\n }\n string res;\n for(char c : baseStr){\n res += m[find(c-'a')];\n }\n return res;\n }\n\n int find(int x){\n if(parent[x] != x){\n parent[x] = find(parent[x]);\n }\n return parent[x];\n }\n\n void merge(int x, int y){\n int rx = find(x), ry = find(y);\n if(rx != ry){\n if(rank[rx] < rank[ry]) swap(rx,ry);\n parent[ry] = rx;\n if(rank[rx] == rank[ry]) rank[rx]++;\n }\n }\n};",
"memory": "9300"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n int findUPar(int node,vector<int> &parent)\n {\n if(parent[node]==node)return node;\n return parent[node]=findUPar(parent[node],parent);\n }\n void unionBySize(vector<int> &size,int node1,int node2,vector<int> &parent)\n {\n int ulPar1=findUPar(node1,parent);\n int ulPar2=findUPar(node2,parent);\n if(ulPar1==ulPar2)return;\n if(size[ulPar1]>size[ulPar2])\n {\n parent[ulPar2]=ulPar1;\n size[ulPar1]+=size[ulPar2];\n }\n else\n {\n parent[ulPar1]=ulPar2;\n size[ulPar2]+=size[ulPar1];\n }\n }\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n vector<int> size(27,1);\n vector<int> parent(27,0);\n string res;\n for(int i=0;i<27;i++)parent[i]=i;\n for(int i=0;i<s1.size();i++)\n {\n unionBySize(size,s1[i]-'a'+1,s2[i]-'a'+1,parent);\n }\n unordered_map<char,char> mp;\n for(int i=0;i<s1.size();i++)\n {\n int node1=findUPar(s1[i]-'a'+1,parent);\n // int node2=findUPar(s2[i]-'a'+1);\n char minChar=min(s1[i],s2[i]);\n if(mp.find(node1-1+'a')==mp.end())\n {\n mp[node1-1+'a']=minChar;\n }\n else\n {\n minChar=min(minChar,mp[node1-1+'a']);\n mp[node1-1+'a']=minChar;\n }\n }\n for(auto &x:mp)cout<<x.first<<\" \"<<x.second<<endl;\n for(int i=0;i<baseStr.size();i++)\n {\n char ans=findUPar(baseStr[i]-'a'+1,parent)-1+'a';\n if(mp.find(ans)==mp.end()){\n res.push_back(baseStr[i]);\n }\n else\n {\n res.push_back(mp[ans]);\n }\n \n \n }\n return res;\n }\n};",
"memory": "9400"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> rank, par;\n int find(int x){\n if(x == par[x]) return x;\n return par[x] = find(par[x]);\n }\n\n void union_(int x, int y){\n int parx = find(x), pary = find(y);\n if(parx == pary) return;\n if(rank[parx]> rank[pary]){\n swap(parx, pary);\n }\n par[parx] = pary;\n if(rank[parx] == rank[pary]){\n rank[pary]++;\n }\n }\n string smallestEquivalentString(string s1, string s2, string base) {\n rank.resize(26,1);\n for(int i=0; i<26; i++)par.push_back(i);\n int n = s1.size();\n for(int i=0; i<n; i++){\n union_(s1[i]-'a', s2[i]-'a');\n }\n unordered_map<int, priority_queue<char, vector<char>, greater<char>>> pq;\n for(int i=0; i<26; i++){\n pq[find(i)].push('a'+i);\n }\n for(int i=0; i<base.size(); i++){\n base[i] = pq[find(base[i]-'a')].top();\n }\n return base;\n }\n};",
"memory": "9500"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> rank, par;\n int find(int x){\n if(x == par[x]) return x;\n return par[x] = find(par[x]);\n }\n\n void union_(int x, int y){\n int parx = find(x), pary = find(y);\n if(parx == pary) return;\n if(rank[parx]> rank[pary]){\n swap(parx, pary);\n }\n par[parx] = pary;\n if(rank[parx] == rank[pary]){\n rank[pary]++;\n }\n }\n string smallestEquivalentString(string s1, string s2, string base) {\n rank.resize(26,1);\n for(int i=0; i<26; i++)par.push_back(i);\n int n = s1.size();\n for(int i=0; i<n; i++){\n union_(s1[i]-'a', s2[i]-'a');\n }\n unordered_map<int, priority_queue<char, vector<char>, greater<char>>> pq;\n for(int i=0; i<26; i++){\n pq[find(i)].push('a'+i);\n }\n for(int i=0; i<base.size(); i++){\n base[i] = pq[find(base[i]-'a')].top();\n }\n return base;\n }\n};",
"memory": "9600"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> parent;\n vector<int> rank;\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n1 = s1.size(), n2 = baseStr.size();\n for(int i = 0;i<26;i++){\n parent.push_back(i);\n rank.push_back(0);\n }\n unordered_map<char,char> m;\n for(int i = 0;i<n1;i++){\n merge(s1[i]-'a',s2[i]-'a');\n }\n for(int i = 0;i<26;i++){\n if(m.count(find(i)+'a') == 0){\n m[find(i)+'a'] = i+'a';\n }else{\n m[i+'a'] = m[find(i)+'a'];\n }\n }\n string res;\n for(char c : baseStr){\n if(m.count(c) > 0){\n res += m[c];\n }else{\n res +=c;\n }\n }\n return res;\n }\n\n int find(int x){\n if(parent[x] != x){\n parent[x] = find(parent[x]);\n }\n return parent[x];\n }\n\n void merge(int x, int y){\n int rx = find(x), ry = find(y);\n if(rx != ry){\n if(rank[rx] < rank[ry]) swap(rx,ry);\n parent[ry] = rx;\n if(rank[rx] == rank[ry]) rank[rx]++;\n }\n }\n};",
"memory": "9700"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Disjointset{\n vector<int> parent, size; \npublic: \n Disjointset(int n){\n parent.resize(n+1);\n size.resize(n+1);\n for(int i=0;i<=n;i++){\n size[i]=1;\n parent[i]=i;\n }\n \n }\n int findupar(int u){\n if(u==parent[u])\n return u ;\n return parent[u]=findupar(parent[u]);\n }\n void unionbysize(int u,int v){\n int ulp_u=findupar(u);\n int ulp_v=findupar(v);\n if(ulp_u == ulp_v) return ;\n if(size[ulp_u]<size[ulp_v]){\n parent[ulp_u]=ulp_v;\n size[ulp_v]+= size[ulp_u];\n }\n else{\n parent[ulp_v]=ulp_u ;\n size[ulp_u]+= size[ulp_v] ;\n }\n }\n\n};\nclass Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string b) {\n int n=s1.length();\n int nb=b.length();\n Disjointset ds(26);\n for(int i=0;i<n;i++){\n ds.unionbysize(s1[i]-'a',s2[i]-'a');\n }\n map<int,int> mp;\n for(int i=0;i<26;i++){\n int par=ds.findupar(i);\n if(mp.count(par))\n mp[par]=min(mp[par],i);\n else\n mp[par]=i;\n }\n string ans=\"\";\n for(int i=0;i<nb;i++){\n auto it=mp[ds.findupar(b[i]-'a')];\n ans.push_back('a'+it);\n\n }\n return ans; \n\n }\n};",
"memory": "9800"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n class UnionSet{\n public:\n vector<int> par;\n vector<int> siz;\n UnionSet(int n)\n {\n par.resize(n);\n siz.resize(n, 1);\n for(int i=0; i<n; i++)\n par[i]=i;\n }\n\n int upar(int a)\n {\n if(a==par[a])\n return a;\n \n return par[a]=upar(par[a]);\n }\n\n void join(int a, int b)\n {\n int upa=upar(a);\n int upb=upar(b);\n\n if(upa==upb)\n return;\n if(siz[upa]>=siz[upb])\n {\n siz[upa]+=siz[upb];\n par[upb]=upa;\n }\n else\n {\n siz[upb]+=siz[upa];\n par[upa]=upb;\n }\n }\n unordered_map<char, vector<char>> grp()\n {\n unordered_map<char, vector<char>> mp;\n for(int i=0; i<26; i++)\n mp['a'+upar(i)].push_back('a'+i);\n \n for(auto i: mp)\n sort(mp[i.first].begin(), mp[i.first].end());\n\n return mp;\n }\n };\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n \n UnionSet us(26);\n \n for(int i=0; i<s1.length(); i++)\n us.join(s1[i]-'a', s2[i]-'a');\n\n unordered_map<char, vector<char>> mp=us.grp();\n\n for(auto i: mp)\n {\n cout<<i.first<<\"->\";\n for(auto j: i.second)\n cout<<j<<\" \";\n cout<<endl;\n }\n\n for(int i=0; i<baseStr.size(); i++)\n {\n char p=us.upar(baseStr[i]-'a')+'a';\n\n baseStr[i]=mp[p][0];\n }\n\n return baseStr;\n \n }\n};",
"memory": "9800"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> parent;\n vector<int> rank;\n void Union(int x,int y){\n int x_parent=find(x);\n int y_parent=find(y);\n if(x_parent==y_parent)return ;\n if(rank[x_parent] < rank[y_parent]){\n parent[x_parent]=y_parent;\n }else if(rank[x_parent] > rank[y_parent]){\n parent[y_parent]=x_parent;\n }else{\n parent[x_parent]=y_parent;\n rank[x_parent]++;\n }\n }\n int find(int x){\n int x_parent=parent[x];\n if(x_parent==x){\n return x;\n }\n return parent[x]=find(parent[x]);\n }\n string smallestEquivalentString(string s1, string s2, string br) {\n parent.resize(26);\n rank.resize(26,0);\n for(int i=0 ;i<26;i++){\n parent[i]=i;\n }\n for(int i=0 ;i<s1.size();i++){\n Union(s1[i]-'a',s2[i]-'a');\n }\n unordered_map<int,vector<char>> mp;\n for(int i=0 ;i<26;i++){\n int x=find(i);\n mp[x].push_back(char('a'+i));\n }\n for(int i=0 ;i<26;i++){\n sort(mp[i].begin(),mp[i].end());\n }\n string stri;\n for(int i=0 ;i<br.size();i++){\n int x=find(br[i]-'a');\n stri.push_back(mp[x][0]);\n }\n return stri;\n }\n};",
"memory": "9900"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n map<char,vector<char>>mp;\n\n for (char c = 'a'; c <= 'z'; c++) {\n mp[c].push_back(c);\n }\n\n\n for(int h=0;h<3;h++){\n for(int i=0;i<s1.size();i++){\n\n for (auto it : mp[s2[i]]) {\n if (find(mp[s1[i]].begin(), mp[s1[i]].end(), it) == mp[s1[i]].end()) {\n mp[s1[i]].push_back(it);\n }\n }\n\n for (auto it : mp[s1[i]]) {\n if (find(mp[s2[i]].begin(), mp[s2[i]].end(), it) == mp[s2[i]].end()) {\n mp[s2[i]].push_back(it);\n }\n }\n }\n }\n\n for(int i=0;i<s1.size();i++){\n\n for (auto it : mp[s2[i]]) {\n if (find(mp[s1[i]].begin(), mp[s1[i]].end(), it) == mp[s1[i]].end()) {\n mp[s1[i]].push_back(it);\n }\n }\n\n for (auto it : mp[s1[i]]) {\n if (find(mp[s2[i]].begin(), mp[s2[i]].end(), it) == mp[s2[i]].end()) {\n mp[s2[i]].push_back(it);\n }\n }\n }\n\n for(auto &i : mp){\n sort(i.second.begin(),i.second.end());\n }\n\n string ans=\"\";\n for(int i=0;i<baseStr.size();i++){\n if (mp.find(baseStr[i]) != mp.end()) {\n ans += mp[baseStr[i]][0];\n }\n }\n return ans;\n }\n};",
"memory": "10300"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n vector<int> alpha(26,-1);\n function<int(int)> get=[&](int ind){\n return (alpha[ind]<0?ind:alpha[ind]=get(alpha[ind]));\n };\n function<bool(int,int)> add=[&](int a, int b){\n int curr[2]={get(a),get(b)};\n if(curr[0]==curr[1]) return true;\n if(curr[0]>curr[1]){\n swap(a,b);\n swap(curr[0],curr[1]);\n }\n alpha[curr[0]]+=alpha[curr[1]];\n alpha[curr[1]]=curr[0];\n return false;\n };\n for(int i=0;i<s1.length();i++){\n add(s1[i]-'a',s2[i]-'a');\n }\n for(int i=0;i<baseStr.length();i++){\n baseStr[i]=char(get(baseStr[i]-'a')+'a');\n }\n return baseStr;\n }\n};",
"memory": "10400"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n vector<int> alpha(26,-1);\n function<int(int)> get=[&](int ind){\n return (alpha[ind]<0?ind:alpha[ind]=get(alpha[ind]));\n };\n function<bool(int,int)> add=[&](int a, int b){\n int curr[2]={get(a),get(b)};\n if(curr[0]==curr[1]) return true;\n if(curr[0]>curr[1]){\n swap(a,b);\n swap(curr[0],curr[1]);\n }\n alpha[curr[0]]+=alpha[curr[1]];\n alpha[curr[1]]=curr[0];\n return false;\n };\n for(int i=0;i<s1.length();i++){\n add(s1[i]-'a',s2[i]-'a');\n }\n for(int i=0;i<baseStr.length();i++){\n baseStr[i]=char(get(baseStr[i]-'a')+'a');\n }\n return baseStr;\n }\n};",
"memory": "10400"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n std::string smallestEquivalentString(std::string s1, std::string s2, std::string baseStr) {\n std::vector<char> map;\n std::vector<std::set<int>> tmpMap;\n tmpMap.resize(26);\n for (int i = 0; i < 26; i++) {\n tmpMap[i].insert(i);\n } \n for (int i = 0; i < s1.length(); i++) {\n tmpMap[s1[i] - 'a'].insert(s2[i] - 'a');\n tmpMap[s2[i] - 'a'].insert(s1[i] - 'a');\n }\n bool change = true;\n while (change) {\n change = false;\n for (int i = 0; i < 26; i++) {\n for (int j = 0; j < 26; j++) {\n if (tmpMap[j].find(i) != tmpMap[j].end()) {\n for (auto x : tmpMap[j]) {\n if (change) { tmpMap[i].insert(x); }\n else { change = (tmpMap[i].insert(x)).second; } \n }\n }\n }\n }\n }\n for (int i = 0; i < 26; i++) {\n map.push_back('a' + *(tmpMap[i].begin()));\n }\n std::string result = \"\";\n for (auto x : baseStr) {\n result.push_back(map[x - 'a']);\n }\n return result;\n }\n};",
"memory": "10500"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n std::string smallestEquivalentString(std::string s1, std::string s2, std::string baseStr) {\n std::vector<char> map;\n std::vector<std::set<int>> tmpMap;\n tmpMap.resize(26);\n for (int i = 0; i < 26; i++) {\n tmpMap[i].insert(i);\n } \n for (int i = 0; i < s1.length(); i++) {\n tmpMap[s1[i] - 'a'].insert(s2[i] - 'a');\n tmpMap[s2[i] - 'a'].insert(s1[i] - 'a');\n }\n bool change = true;\n while (change) {\n change = false;\n for (int i = 0; i < 26; i++) {\n for (int j = 0; j < 26; j++) {\n if (tmpMap[j].find(i) != tmpMap[j].end()) {\n for (auto x : tmpMap[j]) {\n if (change) { tmpMap[i].insert(x); }\n else { change = (tmpMap[i].insert(x)).second; } \n }\n }\n }\n }\n }\n for (int i = 0; i < 26; i++) {\n map.push_back('a' + *(tmpMap[i].begin()));\n }\n std::string result = \"\";\n for (auto x : baseStr) {\n result.push_back(map[x - 'a']);\n }\n return result;\n }\n};",
"memory": "10600"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n std::string smallestEquivalentString(std::string s1, std::string s2, std::string baseStr) {\n std::vector<char> map;\n std::vector<std::set<int>> tmpMap;\n tmpMap.resize(26);\n for (int i = 0; i < 26; i++) {\n tmpMap[i].insert(i);\n } \n for (int i = 0; i < s1.length(); i++) {\n tmpMap[s1[i] - 'a'].insert(s2[i] - 'a');\n tmpMap[s2[i] - 'a'].insert(s1[i] - 'a');\n }\n bool change = true;\n while (change) {\n change = false;\n for (int i = 0; i < 26; i++) {\n for (int j = 0; j < 26; j++) {\n if (tmpMap[j].find(i) != tmpMap[j].end()) {\n for (auto x : tmpMap[j]) {\n if (change) { tmpMap[i].insert(x); }\n else { change = (tmpMap[i].insert(x)).second; } \n }\n }\n }\n }\n }\n for (int i = 0; i < 26; i++) {\n map.push_back('a' + *(tmpMap[i].begin()));\n }\n std::string result = \"\";\n for (auto x : baseStr) {\n result.push_back(map[x - 'a']);\n }\n return result;\n }\n};",
"memory": "10700"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\n char dfs(char currChar, vector<vector<char>>& adj, vector<int>& visited) {\n visited[currChar - 'a'] = 1;\n char minChar = currChar;\n for (auto &neighbour : adj[currChar - 'a']) {\n if (!visited[neighbour - 'a']) {\n minChar = min(minChar, dfs(neighbour, adj, visited));\n }\n }\n return minChar;\n }\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n = s1.size();\n vector<vector<char>> adj(26);\n for (int i = 0; i < n; i++) {\n adj[s1[i] - 'a'].push_back(s2[i]);\n adj[s2[i] - 'a'].push_back(s1[i]);\n }\n\n int m = baseStr.size();\n string ans = \"\";\n for (int i = 0; i < m; i++) {\n char currChar = baseStr[i];\n vector<int> visited(26, 0);\n char minChar = dfs(currChar, adj, visited);\n ans.push_back(minChar);\n }\n \n return ans;\n }\n};\n",
"memory": "10800"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n char dfs(char s, vector<int> adj[], vector<int>& vis) {\n vis[s - 'a'] = 1; // Mark the current character as visited\n char mini = s; // Initialize the smallest character as the current one\n \n for (auto it : adj[s - 'a']) {\n if (!vis[it]) {\n mini = min(mini, dfs(it+'a' , adj, vis)); // Convert index back to char\n }\n }\n return mini;\n }\n\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n = s1.size();\n vector<int> adj[26]; // 26 letters (from 'a' to 'z')\n\n // Build the adjacency list\n for (int i = 0; i < n; i++) {\n char a = s1[i];\n char b = s2[i];\n adj[a - 'a'].push_back(b - 'a'); // Store as indices\n adj[b - 'a'].push_back(a - 'a');\n }\n\n string result;\n\n // For each character in baseStr, find the smallest equivalent character\n for (char c : baseStr) {\n vector<int> vis(26, 0); // Reset visited array for each character\n char smallest = dfs(c, adj, vis); // Find the smallest equivalent character\n result.push_back(smallest); // Add to result\n }\n\n return result;\n }\n};\n",
"memory": "10900"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 3 | {
"code": "// class DisjointSet {\n// vector<int> rank, parent, size;\n// public:\n// DisjointSet(int n) {\n// rank.resize(n + 1, 0);\n// parent.resize(n + 1);\n// size.resize(n + 1);\n// for (int i = 0; i <= n; i++) {\n// parent[i] = i;\n// size[i] = 1;\n// }\n// } \n\n// int findPar(int node) {\n// if (node == parent[node])\n// return node;\n// return parent[node] = findPar(parent[node]);\n// }\n\n// void unionByOrder(int u, int v) {\n// int ulp_u = findPar(u);\n// int ulp_v = findPar(v);\n// if (ulp_u == ulp_v) return;\n// if (ulp_u - 'a' < ulp_v - 'a') {\n// parent[ulp_v] = ulp_u;\n// size[ulp_v] += size[ulp_u];\n// }\n// else {\n// parent[ulp_u] = ulp_v;\n// size[ulp_u] += size[ulp_v];\n// }\n// }\n// };\nclass Solution {\npublic:\n void dfs(int node, vector<int> adj[], vector<int>&vis, int& pNode){\n vis[node] = true;\n if(node < pNode)pNode = node;\n\n for(auto it: adj[node]){\n if(!vis[it]){\n dfs(it, adj, vis, pNode);\n }\n }\n }\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n = baseStr.length();\n int m = s1.length();\n vector<int> adj[26];\n\n for(int i=0; i<m; i++){\n int u = s1[i];\n int v = s2[i];\n\n adj[u - 'a'].push_back(v - 'a');\n adj[v - 'a'].push_back(u - 'a');\n }\n string s = \"\";\n for(int i=0; i<n; i++){\n vector<int> vis(26, 0);\n int curr = baseStr[i] - 'a';\n int pNode = INT_MAX;\n dfs(curr, adj, vis, pNode);\n cout<<pNode<<endl;\n char ch = 'a' + pNode; \n s.push_back(ch);\n }\n return s;\n }\n};",
"memory": "11000"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 3 | {
"code": "#include <vector>\n#include <string>\n#include <algorithm>\n\nusing namespace std;\n\nclass Solution {\npublic:\n void dfs(int node, int parent, vector<vector<int>>& adj, vector<bool>& visited, int& minChar) {\n visited[node] = true;\n minChar = min(minChar, node);\n\n for (int neighbor : adj[node]) {\n if (!visited[neighbor]) {\n dfs(neighbor, node, adj, visited, minChar);\n }\n }\n }\n\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n vector<vector<int>> adj(26);\n\n for (int i = 0; i < s1.size(); i++) {\n int u = s1[i] - 'a';\n int v = s2[i] - 'a';\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n string result = baseStr;\n for (int i = 0; i < baseStr.size(); i++) {\n char ch = baseStr[i];\n int node = ch - 'a';\n vector<bool> visited(26, false);\n int minChar = 27; // A value higher than the highest possible index (25)\n dfs(node, -1, adj, visited, minChar);\n result[i] = minChar + 'a';\n }\n\n return result;\n }\n};\n",
"memory": "11100"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n char dfs(map<char, vector<char>>& adj, char current, vector<bool>& visited) {\n visited[current - 'a'] = true; // Mark current node as visited\n char minchar = current; // Initialize with the current character\n \n for (auto it : adj[current]) {\n if (!visited[it - 'a']) {\n minchar = min(dfs(adj, it, visited), minchar); // Update minchar based on DFS\n }\n }\n \n return minchar;\n }\n \n string smallestEquivalentString(string s1, string s2, string baseStr) {\n map<char, vector<char>> adj; // Adjacency list to store the equivalence classes\n int n = s1.length();\n \n // Build the adjacency list\n for (int i = 0; i < n; i++) {\n char u = s1[i];\n char v = s2[i];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n int m = baseStr.length();\n string result;\n \n // For each character in baseStr, find the lexicographically smallest equivalent\n for (int i = 0; i < m; i++) {\n vector<bool> visited(26, false); // Track visited nodes (letters a-z)\n char curr = baseStr[i];\n \n result.push_back(dfs(adj, curr, visited)); // Get smallest equivalent character\n }\n \n return result;\n }\n};\n",
"memory": "11200"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution{\npublic:\n\n void dfs(char u, unordered_map<char, vector<char>>& adj, vector<bool>& vis, char& minChar){\n vis[u-'a']=true;\n minChar=min(minChar, u);\n for(char v: adj[u]){\n if(!vis[v-'a']){\n dfs(v, adj, vis, minChar);\n }\n }\n}\n\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n \n int n=s1.size();\n unordered_map<char, vector<char>> adj;\n for(int i=0; i<n; i++){\n char u=s1[i];\n char v=s2[i];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n int m=baseStr.size();\n string ans=\"\";\n for(int i=0; i<m; i++){\n vector<bool> vis(26, false);\n char ch=baseStr[i];\n char minChar=ch;\n dfs(ch, adj, vis, minChar);\n ans+=minChar;\n }\n\n return ans;\n }\n};",
"memory": "11300"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n void dfs(char u, unordered_map<char, vector<char>>& adj, vector<bool>& vis, char& ch){\n vis[u-'a']=true;\n ch=min(u, ch);\n for(auto v: adj[u]){\n if(!vis[v-'a']){\n dfs(v, adj, vis, ch);\n }\n }\n\n }\n \n \n string smallestEquivalentString(string s1, string s2, string baseStr) {\n \n int n=s1.size();\n unordered_map<char, vector<char>> adj;\n\n for(int i=0; i<n; i++){\n \n adj[s1[i]].push_back(s2[i]);\n if(s1[i]!=s2[i]){\n adj[s2[i]].push_back(s1[i]);\n }\n // else{\n // adj[s1[i]].push_back('{');\n // }\n }\n\n for(auto v: adj['m']){\n // int o=v.size();\n // cout<<endl;\n // for(int i=0; i<o; i++){\n cout<<v<<\" \";\n // }\n // cout<<endl;\n }\n \n \n int m=baseStr.size();\n string ans=\"\";\n for(int i=0; i<m; i++){\n vector<bool> vis(26, false);\n char ch='z';\n dfs(baseStr[i], adj, vis, ch);\n ans+=ch;\n vis.clear();\n // ch.clear();\n }\n return ans;\n // return -1;\n }\n};",
"memory": "11400"
} |
1,058 | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n void dfs(char u, unordered_map<char, vector<char>>& adj, vector<bool>& vis, char& ch){\n vis[u-'a']=true;\n ch=min(u, ch);\n for(auto v: adj[u]){\n if(!vis[v-'a']){\n dfs(v, adj, vis, ch);\n }\n }\n\n }\n \n \n //Question function:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n \n int n=s1.size();\n unordered_map<char, vector<char>> adj;\n\n for(int i=0; i<n; i++){\n \n adj[s1[i]].push_back(s2[i]);\n if(s1[i]!=s2[i])//However this if is not required but using it will slightly optimize our code.\n adj[s2[i]].push_back(s1[i]);\n }\n \n int m=baseStr.size();\n string ans=\"\";\n for(int i=0; i<m; i++){\n vector<bool> vis(26, false);\n char ch='z';\n dfs(baseStr[i], adj, vis, ch);\n ans+=ch;\n }\n return ans;\n }\n};",
"memory": "11400"
} |
End of preview. Expand
in Dataset Viewer.
README.md exists but content is empty.
- Downloads last month
- 38