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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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" }
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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 //DFS appraoch: \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};\n\n\n\n//Using BFS approach:\n/*\nclass Solution{\npublic:\n\n //BFS logic:\n char bfs(char u, unordered_map<char, vector<char>>& adj){\n \n char minChar=u;\n vector<bool> vis(26, false);\n queue<char> q;\n q.push(u);\n vis[u-'a']=true;\n while(!q.empty()){\n char node=q.front();\n q.pop();\n minChar=min(minChar, node);\n for(char v: adj[node]){\n if(!vis[v-'a']){\n vis[v-'a']=true;\n q.push(v);\n }\n }\n }\n return minChar;\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 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=bfs(ch, adj);\n ans+=minChar;\n }\n\n return ans;\n }\n};\n*/", "memory": "11500" }
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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 void dfs(unordered_map<char,vector<char>>&adj,string &basestr,char &ch,vector<bool>&vis,char &mini){\n vis[ch-'a']=1;\n mini=min(mini,ch);\n\n for(char &it:adj[ch]){\n if(vis[it-'a']) continue;\n dfs(adj,basestr,it,vis,mini);\n } \n }\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n=s1.size();\n unordered_map<char,vector<char>>adj;\n\n for(int i=0;i<n;i++){\n char u=s1[i],v=s2[i];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n string s=\"\";\n \n for(int i=0;i<baseStr.size();i++){\n char ch=baseStr[i];\n char min=CHAR_MAX;\n vector<bool>vis(26,0);\n dfs(adj,baseStr,ch,vis,min);\n s+=min;\n\n }\n return s;\n }\n};", "memory": "11600" }
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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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(unordered_map<char, vector<char>> &adj, char curr, vector<int>& visited) {\n visited[curr-'a'] = 1;\n \n char minChar = curr;\n \n for(char &v : adj[curr]) {\n \n if(visited[v-'a'] == 0)\n minChar = min(minChar, DFS(adj, v, visited));\n }\n \n return minChar;\n }\n \n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n = s1.length();\n unordered_map<char, vector<char>> adj;\n \n for(int i = 0; i<n; i++) {\n char u = s1[i];\n char v = s2[i];\n \n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n \n int m = baseStr.length();\n string result;\n \n for(int i = 0; i<m; i++) {\n char ch = baseStr[i];\n \n vector<int> visited(26, 0);\n \n result.push_back(DFS(adj, ch, visited));\n }\n \n return result;\n }\n};\n", "memory": "11700" }
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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 DSU{\n private:\n vector<int>parent;\n int n;\n\n public:\n DSU()\n {\n n=26;\n parent.resize(26,0);\n\n for(int i=0;i<26;i++)\n parent[i]=i;\n }\n\n int find(int i)\n {\n if(parent[i]==i)\n return i;\n\n return parent[i]=find(parent[i]);\n }\n\n void U(int a,int b)\n {\n int ap=find(a);\n int bp=find(b);\n\n if(ap!=bp)\n {\n if(ap<bp)\n parent[bp]=ap;\n\n else\n parent[ap]=bp;\n }\n }\n};\n\nclass Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n\n int n=s1.size();\n string ans=\"\";\n\n DSU dsu;\n\n\n for(int i=0;i<n;i++)\n dsu.U(s1[i]-'a',s2[i]-'a');\n\n int m=baseStr.size();\n\n for(int i=0;i<m;i++)\n {\n ans=ans+char('a'+dsu.find(baseStr[i]-'a'));\n }\n return ans;\n }\n};", "memory": "11800" }
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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 DSU{\n private:\n vector<int>parent;\n int n;\n\n public:\n DSU()\n {\n n=26;\n parent.resize(26,0);\n\n for(int i=0;i<26;i++)\n parent[i]=i;\n }\n\n int find(int i)\n {\n if(parent[i]==i)\n return i;\n\n return parent[i]=find(parent[i]);\n }\n\n void U(int a,int b)\n {\n int ap=find(a);\n int bp=find(b);\n\n if(ap!=bp)\n {\n if(ap<bp)\n parent[bp]=ap;\n\n else\n parent[ap]=bp;\n }\n }\n};\n\nclass Solution {\npublic:\n Solution() {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(NULL);\n std::cout.tie(NULL);\n }\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n\n int n=s1.size();\n string ans=\"\";\n\n DSU dsu;\n\n for(int i=0;i<n;i++)\n dsu.U(s1[i]-'a',s2[i]-'a');\n\n int m=baseStr.size();\n\n for(int i=0;i<m;i++)\n {\n ans=ans+char('a'+dsu.find(baseStr[i]-'a'));\n }\n return ans;\n }\n};", "memory": "11900" }
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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 char DFS_find_min_char(unordered_map<char,vector<char>>&adj,char curr_ch,vector<int>&visited){\n visited[curr_ch-'a']=1;//mark it visited;\n char minChar=curr_ch;\n for(char &v:adj[curr_ch]){\n if(visited[v-'a']==0){\n minChar=min(minChar,DFS_find_min_char(adj,v,visited));\n }\n }\n return minChar;\n }\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n=s1.length();\n int m=baseStr.length();\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 string result;\n //hr ek string k char l liyr ek new DFS call hoga \n for(int i=0;i<m;i++){\n char ch=baseStr[i];\n vector<int>visited(26,0);//for this ch none is visited as of now \n char minChar=DFS_find_min_char(adj,ch,visited);\n result.push_back(minChar);\n }\n return result;\n }\n\n};", "memory": "12000" }
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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 char DFS_find_min_char(unordered_map<char,vector<char>>&adj,char curr_ch,vector<int>&visited){\n visited[curr_ch-'a']=1;//mark it visited;\n char minChar=curr_ch;\n for(char &v:adj[curr_ch]){\n if(visited[v-'a']==0){\n minChar=min(minChar,DFS_find_min_char(adj,v,visited));\n }\n }\n return minChar;\n }\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n=s1.length();\n int m=baseStr.length();\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 string result;\n //hr ek string k char l liyr ek new DFS call hoga \n for(int i=0;i<m;i++){\n char ch=baseStr[i];\n vector<int>visited(26,0);//for this ch none is visited as of now \n char minChar=DFS_find_min_char(adj,ch,visited);\n result.push_back(minChar);\n }\n return result;\n }\n\n};", "memory": "12000" }
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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 char DFS(unordered_map<char, vector<char>> &adj, char curr, vector<int>& visited) {\n visited[curr-'a'] = 1;\n \n char minChar = curr;\n \n for(char &v : adj[curr]) {\n \n if(visited[v-'a'] == 0)\n minChar = min(minChar, DFS(adj, v, visited));\n }\n \n return minChar;\n }\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n = s1.length();\n unordered_map<char, vector<char>> adj;\n \n for(int i = 0; i<n; i++) {\n char u = s1[i];\n char v = s2[i];\n \n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n \n int m = baseStr.length();\n string result;\n \n for(int i = 0; i<m; i++) {\n char ch = baseStr[i];\n \n vector<int> visited(26, 0);\n \n result.push_back(DFS(adj, ch, visited));\n }\n \n return result;\n\n }\n};", "memory": "12100" }
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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 int dfs(int index, vector<int>& visited,unordered_map<int, vector<int>>& adjList) {\n visited[index] = 1;\n int min_char = index;\n for (auto& v : adjList[min_char]) {\n if (visited[v] == 0) {\n min_char = min(min_char, dfs(v, visited, adjList));\n }\n }\n return min_char;\n }\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n = baseStr.length();\n int m = s1.length();\n unordered_map<int, vector<int>> adjList;\n for (int i = 0; i < m; i++) {\n int u = s1[i] - 'a';\n int v = s2[i] - 'a';\n adjList[u].push_back(v);\n adjList[v].push_back(u);\n }\n string ans = \"\";\n for (int i = 0; i < n; i++) {\n vector<int> visited(26, 0);\n int result = dfs(baseStr[i] - 'a', visited, adjList);\n ans.push_back(result + 'a');\n }\n return ans;\n }\n};", "memory": "12200" }
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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 char DFS( char ch , vector<int> &vis , unordered_map<char,set<char>> &adj )\n {\n vis[ch-'a'] = 1;\n char minChar = ch;\n for( auto it : adj[ch] )\n {\n if( vis[it - 'a'] == -1 )\n {\n ch = min( ch , DFS(it , vis , adj ) );\n }\n }\n return ch;\n }\n string smallestEquivalentString(string s1, string s2, string str) \n {\n int size = s1.size() ;\n string ans;\n unordered_map<char,set<char>> adj;\n for( int i = 0 ; i < size ; i++ )\n {\n char u = s1[i] , v = s2[i] ;\n adj[u].insert(v);\n adj[v].insert(u);\n }\n for( int i = 0 ; i < str.size() ; i++ )\n {\n vector<int> vis(26,-1);\n ans.push_back( DFS( str[i] , vis , adj ) ) ;\n }\n return ans;\n }\n};", "memory": "12300" }
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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 char dfs(unordered_map<char,list<char>>&mp,char cur_ch ,vector<bool>&visited){\n visited[cur_ch-'a'] =1;\n char minch =cur_ch;\n for(auto nbr :mp[cur_ch]){\n if(!visited[nbr-'a']){\n minch =min(minch,dfs(mp,nbr,visited));\n }\n }\n return minch;\n }\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n unordered_map<char,list<char>>mp;\n for(int i=0;i<s1.size();i++){\n char u =s1[i];\n char v =s2[i];\n mp[u].push_back(v);\n mp[v].push_back(u);\n }\n\n string ans=\"\";\n for(int i=0;i<baseStr.size();i++){\n vector<bool>visited(26,false);\n char minchar =dfs(mp,baseStr[i],visited);\n ans+=minchar;\n }\n return ans;\n }\n};", "memory": "12400" }
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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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 string smallestEquivalentString(string s1, string s2, string baseStr) {\n vector<vector<int>> edges(26);\n map<pair<char,char>,bool> m;\n for (int i=0;i<s1.size();i++)\n {\n if (m.find(pair(s1[i],s2[i])) == m.end())\n {\n m[pair(s1[i],s2[i])] = true;\n m[pair(s2[i],s1[i])] = true;\n edges[s1[i]-'a'].push_back(s2[i]-'a');\n edges[s2[i]-'a'].push_back(s1[i]-'a');\n }\n }\n vector<int> mapping(26);\n for(int i=0;i<26;i++)\n {\n vector<int> curr;\n curr.push_back(i);\n vector<bool> visited(26,false);\n visited[i] = true;\n int val = i;\n while(curr.size() != 0)\n {\n vector<int> next;\n for (int j=0;j<curr.size();j++)\n {\n for (int k=0;k<edges[curr[j]].size();k++)\n {\n if (!visited[edges[curr[j]][k]])\n {\n visited[edges[curr[j]][k]] = true;\n val = min(val,edges[curr[j]][k]);\n next.push_back(edges[curr[j]][k]);\n }\n }\n }\n curr = next;\n }\n mapping[i] = val;\n }\n string res = \"\";\n for(int i=0;i<baseStr.size();i++)\n res += ((char)mapping[baseStr[i]-'a'])+'a';\n return res;\n }\n};", "memory": "12600" }
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 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li> <li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li> <li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot; <strong>Output:</strong> &quot;makkek&quot; <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 &quot;makkek&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot; <strong>Output:</strong> &quot;hdld&quot; <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 &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot; <strong>Output:</strong> &quot;aauaaaaada&quot; <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 &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 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:\nchar dfs( unordered_map<char,list<char>>&adj,char ch,vector<int>&vis){\n vis[ch-'a']=1;\n char minc=ch;\n for(auto i:adj[ch]){\n if(!vis[i-'a']){\n minc=min(minc,dfs(adj,i,vis));\n }\n }\n return minc;\n}\n string smallestEquivalentString(string s1, string s2, string bs) {\n int n=s1.length();\n unordered_map<char,list<char>>adj;\n for(int i=0;i<n;i++ ){\n char ch1=s1[i];\n char ch2=s2[i];\n adj[ch1].push_back(ch2);\n adj[ch2].push_back(ch1);\n }\n string result=\"\";\n int m=bs.length();\n for(int i=0;i<m;i++){\n int ch1=bs[i];\n vector<int>vis(26,0);\n result+=dfs(adj,ch1,vis);\n }\n return result;\n }\n};", "memory": "12700" }
1,984
<p>You are given two <strong>non-increasing 0-indexed </strong>integer arrays <code>nums1</code>​​​​​​ and <code>nums2</code>​​​​​​.</p> <p>A pair of indices <code>(i, j)</code>, where <code>0 &lt;= i &lt; nums1.length</code> and <code>0 &lt;= j &lt; nums2.length</code>, is <strong>valid</strong> if both <code>i &lt;= j</code> and <code>nums1[i] &lt;= nums2[j]</code>. The <strong>distance</strong> of the pair is <code>j - i</code>​​​​.</p> <p>Return <em>the <strong>maximum distance</strong> of any <strong>valid</strong> pair </em><code>(i, j)</code><em>. If there are no valid pairs, return </em><code>0</code>.</p> <p>An array <code>arr</code> is <strong>non-increasing</strong> if <code>arr[i-1] &gt;= arr[i]</code> for every <code>1 &lt;= i &lt; arr.length</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5] <strong>Output:</strong> 2 <strong>Explanation:</strong> The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4). The maximum distance is 2 with pair (2,4). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [2,2,2], nums2 = [10,10,1] <strong>Output:</strong> 1 <strong>Explanation:</strong> The valid pairs are (0,0), (0,1), and (1,1). The maximum distance is 1 with pair (0,1). </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums1 = [30,29,19,5], nums2 = [25,25,25,25,25] <strong>Output:</strong> 2 <strong>Explanation:</strong> The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4). The maximum distance is 2 with pair (2,4). </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums1[i], nums2[j] &lt;= 10<sup>5</sup></code></li> <li>Both <code>nums1</code> and <code>nums2</code> are <strong>non-increasing</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int res = 0;\n int i = 0, j = 0;\n while(i < nums1.size() && j < nums2.size()){\n while(j < nums2.size() && nums1[i] <= nums2[j]){\n j++;\n }\n i++;\n res = max(res, j-i);\n }\n\n return res;\n }\n};", "memory": "100800" }
1,984
<p>You are given two <strong>non-increasing 0-indexed </strong>integer arrays <code>nums1</code>​​​​​​ and <code>nums2</code>​​​​​​.</p> <p>A pair of indices <code>(i, j)</code>, where <code>0 &lt;= i &lt; nums1.length</code> and <code>0 &lt;= j &lt; nums2.length</code>, is <strong>valid</strong> if both <code>i &lt;= j</code> and <code>nums1[i] &lt;= nums2[j]</code>. The <strong>distance</strong> of the pair is <code>j - i</code>​​​​.</p> <p>Return <em>the <strong>maximum distance</strong> of any <strong>valid</strong> pair </em><code>(i, j)</code><em>. If there are no valid pairs, return </em><code>0</code>.</p> <p>An array <code>arr</code> is <strong>non-increasing</strong> if <code>arr[i-1] &gt;= arr[i]</code> for every <code>1 &lt;= i &lt; arr.length</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5] <strong>Output:</strong> 2 <strong>Explanation:</strong> The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4). The maximum distance is 2 with pair (2,4). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [2,2,2], nums2 = [10,10,1] <strong>Output:</strong> 1 <strong>Explanation:</strong> The valid pairs are (0,0), (0,1), and (1,1). The maximum distance is 1 with pair (0,1). </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums1 = [30,29,19,5], nums2 = [25,25,25,25,25] <strong>Output:</strong> 2 <strong>Explanation:</strong> The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4). The maximum distance is 2 with pair (2,4). </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums1[i], nums2[j] &lt;= 10<sup>5</sup></code></li> <li>Both <code>nums1</code> and <code>nums2</code> are <strong>non-increasing</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n ptrdiff_t ans{0};\n auto it1{nums1.begin()}, it2{nums2.begin()};\n while (it1 != nums1.end() && it2 != nums2.end()) {\n if (*it1 > *it2) {\n ++it1;\n }\n else {\n ans = max(ans, distance(nums2.begin(), it2) - distance(nums1.begin(), it1));\n ++it2;\n }\n }\n return ans;\n }\n};", "memory": "100900" }
1,984
<p>You are given two <strong>non-increasing 0-indexed </strong>integer arrays <code>nums1</code>​​​​​​ and <code>nums2</code>​​​​​​.</p> <p>A pair of indices <code>(i, j)</code>, where <code>0 &lt;= i &lt; nums1.length</code> and <code>0 &lt;= j &lt; nums2.length</code>, is <strong>valid</strong> if both <code>i &lt;= j</code> and <code>nums1[i] &lt;= nums2[j]</code>. The <strong>distance</strong> of the pair is <code>j - i</code>​​​​.</p> <p>Return <em>the <strong>maximum distance</strong> of any <strong>valid</strong> pair </em><code>(i, j)</code><em>. If there are no valid pairs, return </em><code>0</code>.</p> <p>An array <code>arr</code> is <strong>non-increasing</strong> if <code>arr[i-1] &gt;= arr[i]</code> for every <code>1 &lt;= i &lt; arr.length</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5] <strong>Output:</strong> 2 <strong>Explanation:</strong> The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4). The maximum distance is 2 with pair (2,4). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [2,2,2], nums2 = [10,10,1] <strong>Output:</strong> 1 <strong>Explanation:</strong> The valid pairs are (0,0), (0,1), and (1,1). The maximum distance is 1 with pair (0,1). </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums1 = [30,29,19,5], nums2 = [25,25,25,25,25] <strong>Output:</strong> 2 <strong>Explanation:</strong> The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4). The maximum distance is 2 with pair (2,4). </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums1[i], nums2[j] &lt;= 10<sup>5</sup></code></li> <li>Both <code>nums1</code> and <code>nums2</code> are <strong>non-increasing</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n const int n1 = nums1.size();\n const int n2 = nums2.size();\n auto i = 0;\n auto j = 0;\n auto ret = 0;\n while (i < n1 && j < n2) {\n if (nums1[i] <= nums2[j]) {\n ret = std::max(ret, j - i);\n ++j;\n } else {\n ++i;\n }\n while (i > j) {\n ++j;\n }\n }\n return ret;\n }\n};", "memory": "100900" }
1,984
<p>You are given two <strong>non-increasing 0-indexed </strong>integer arrays <code>nums1</code>​​​​​​ and <code>nums2</code>​​​​​​.</p> <p>A pair of indices <code>(i, j)</code>, where <code>0 &lt;= i &lt; nums1.length</code> and <code>0 &lt;= j &lt; nums2.length</code>, is <strong>valid</strong> if both <code>i &lt;= j</code> and <code>nums1[i] &lt;= nums2[j]</code>. The <strong>distance</strong> of the pair is <code>j - i</code>​​​​.</p> <p>Return <em>the <strong>maximum distance</strong> of any <strong>valid</strong> pair </em><code>(i, j)</code><em>. If there are no valid pairs, return </em><code>0</code>.</p> <p>An array <code>arr</code> is <strong>non-increasing</strong> if <code>arr[i-1] &gt;= arr[i]</code> for every <code>1 &lt;= i &lt; arr.length</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5] <strong>Output:</strong> 2 <strong>Explanation:</strong> The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4). The maximum distance is 2 with pair (2,4). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [2,2,2], nums2 = [10,10,1] <strong>Output:</strong> 1 <strong>Explanation:</strong> The valid pairs are (0,0), (0,1), and (1,1). The maximum distance is 1 with pair (0,1). </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums1 = [30,29,19,5], nums2 = [25,25,25,25,25] <strong>Output:</strong> 2 <strong>Explanation:</strong> The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4). The maximum distance is 2 with pair (2,4). </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums1[i], nums2[j] &lt;= 10<sup>5</sup></code></li> <li>Both <code>nums1</code> and <code>nums2</code> are <strong>non-increasing</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int i=0,j=0;\n int c=0;\n while(i<nums1.size() && j<nums2.size())\n {\n if(nums1[i]<=nums2[j]){\n c=max(c,j-i);\n j++;\n }\n else{\n i++;\n }\n }\n\n\n\n\nreturn c;\n }\n};", "memory": "101000" }
1,984
<p>You are given two <strong>non-increasing 0-indexed </strong>integer arrays <code>nums1</code>​​​​​​ and <code>nums2</code>​​​​​​.</p> <p>A pair of indices <code>(i, j)</code>, where <code>0 &lt;= i &lt; nums1.length</code> and <code>0 &lt;= j &lt; nums2.length</code>, is <strong>valid</strong> if both <code>i &lt;= j</code> and <code>nums1[i] &lt;= nums2[j]</code>. The <strong>distance</strong> of the pair is <code>j - i</code>​​​​.</p> <p>Return <em>the <strong>maximum distance</strong> of any <strong>valid</strong> pair </em><code>(i, j)</code><em>. If there are no valid pairs, return </em><code>0</code>.</p> <p>An array <code>arr</code> is <strong>non-increasing</strong> if <code>arr[i-1] &gt;= arr[i]</code> for every <code>1 &lt;= i &lt; arr.length</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5] <strong>Output:</strong> 2 <strong>Explanation:</strong> The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4). The maximum distance is 2 with pair (2,4). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [2,2,2], nums2 = [10,10,1] <strong>Output:</strong> 1 <strong>Explanation:</strong> The valid pairs are (0,0), (0,1), and (1,1). The maximum distance is 1 with pair (0,1). </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums1 = [30,29,19,5], nums2 = [25,25,25,25,25] <strong>Output:</strong> 2 <strong>Explanation:</strong> The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4). The maximum distance is 2 with pair (2,4). </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums1[i], nums2[j] &lt;= 10<sup>5</sup></code></li> <li>Both <code>nums1</code> and <code>nums2</code> are <strong>non-increasing</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int n = nums1.size(), m = nums2.size();\n int i = 0, j = 0, maxi = 0;\n while(i < n && j < m) {\n if(nums1[i] > nums2[j]) {\n i++;\n }\n else {\n maxi = max(maxi, j-i);\n j++;\n }\n }\n return maxi;\n }\n};", "memory": "101100" }
1,984
<p>You are given two <strong>non-increasing 0-indexed </strong>integer arrays <code>nums1</code>​​​​​​ and <code>nums2</code>​​​​​​.</p> <p>A pair of indices <code>(i, j)</code>, where <code>0 &lt;= i &lt; nums1.length</code> and <code>0 &lt;= j &lt; nums2.length</code>, is <strong>valid</strong> if both <code>i &lt;= j</code> and <code>nums1[i] &lt;= nums2[j]</code>. The <strong>distance</strong> of the pair is <code>j - i</code>​​​​.</p> <p>Return <em>the <strong>maximum distance</strong> of any <strong>valid</strong> pair </em><code>(i, j)</code><em>. If there are no valid pairs, return </em><code>0</code>.</p> <p>An array <code>arr</code> is <strong>non-increasing</strong> if <code>arr[i-1] &gt;= arr[i]</code> for every <code>1 &lt;= i &lt; arr.length</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5] <strong>Output:</strong> 2 <strong>Explanation:</strong> The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4). The maximum distance is 2 with pair (2,4). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [2,2,2], nums2 = [10,10,1] <strong>Output:</strong> 1 <strong>Explanation:</strong> The valid pairs are (0,0), (0,1), and (1,1). The maximum distance is 1 with pair (0,1). </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums1 = [30,29,19,5], nums2 = [25,25,25,25,25] <strong>Output:</strong> 2 <strong>Explanation:</strong> The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4). The maximum distance is 2 with pair (2,4). </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums1[i], nums2[j] &lt;= 10<sup>5</sup></code></li> <li>Both <code>nums1</code> and <code>nums2</code> are <strong>non-increasing</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\nint maxDistance(vector<int>& n1, vector<int>& n2) {\n\n int ans=0,k=min(n1.size(),n2.size());\n for(int i=0; i<k;i++){\n int l=i,r=n2.size()-1,m;\n while(l<=r){\n m=l+(r-l)/2;\n int t= n1[i];\n if(n2[m]>=t){\n l=m+1;\n if(m-i>ans)ans=m-i;\n } \n else r=m-1;\n } \n }\n return ans;\n }\n};", "memory": "101100" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n vector<int> g[n];\r\n int indegree[n];\r\n int node_color[n][26];\r\n memset(indegree, 0, sizeof(indegree));\r\n memset(node_color, 0, sizeof(node_color));\r\n for(auto& edge: edges){\r\n int x = edge[0];\r\n int y = edge[1];\r\n g[x].push_back(y);\r\n indegree[y]++;\r\n }\r\n\r\n queue<int> q;\r\n for(int i=0; i<n; i++) if(indegree[i]==0) q.push(i);\r\n int remain = n;\r\n int max_color_cnt = 0;\r\n while(!q.empty()){\r\n auto cur = q.front(); q.pop();\r\n remain--;\r\n int cur_color = colors[cur]-'a';\r\n node_color[cur][cur_color]++;\r\n max_color_cnt = max(max_color_cnt, node_color[cur][cur_color]);\r\n\r\n for(auto neighbor: g[cur]){\r\n indegree[neighbor]--;\r\n if(indegree[neighbor]==0) q.push(neighbor);\r\n\r\n for(int color=0; color<26; color++){\r\n node_color[neighbor][color] = max(node_color[neighbor][color], node_color[cur][color]);\r\n }\r\n }\r\n }\r\n\r\n if(remain!=0) return -1;\r\n return max_color_cnt;\r\n }\r\n};", "memory": "127939" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n\r\n int node_color[n][26];\r\n int indegree[n];\r\n memset(indegree, 0, sizeof(indegree));\r\n memset(node_color, 0, sizeof(node_color));\r\n vector<int> g[n];\r\n for(auto& edge: edges){\r\n int x = edge[0];\r\n int y = edge[1];\r\n g[x].push_back(y);\r\n indegree[y]++;\r\n }\r\n\r\n queue<int> q;\r\n for(int i=0; i<n; i++) if(indegree[i]==0) q.push(i);\r\n\r\n int max_color_cnt = 0;\r\n int rest = n;\r\n while(!q.empty()){\r\n auto cur = q.front(); q.pop();\r\n int color = colors[cur]-'a';\r\n rest--;\r\n\r\n node_color[cur][color]++;\r\n max_color_cnt = max(max_color_cnt, node_color[cur][color]);\r\n for(auto& neighbor:g[cur]){\r\n indegree[neighbor]--;\r\n for(int i=0; i<26; i++){\r\n node_color[neighbor][i] = max(node_color[neighbor][i], node_color[cur][i]);\r\n }\r\n\r\n if(indegree[neighbor]==0) q.push(neighbor);\r\n }\r\n }\r\n\r\n if(rest!=0) return -1;\r\n return max_color_cnt;\r\n }\r\n};", "memory": "127939" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> g[100005];\n int n, xl[100005], indeg[100005], f[100005], q[100005];\n string C;\n \n bool topo(){\n int head = 0, tail = 0, i, j;\n for (i = 0; i < n; ++i)\n if (indeg[i] == 0) xl[++tail] = i;\n while (head < tail){\n i = xl[++head];\n for (j = 0; j < g[i].size(); ++j)\n if (--indeg[g[i][j]] == 0) xl[++tail] = g[i][j];\n }\n\n return tail == n;\n }\n \n int largestPathValue(string CC, vector<vector<int>>& edges) {\n int i, j, c, res = 0;\n C = CC;\n n = CC.size();\n for (i = 0; i < n; ++i) g[i].clear();\n for (i = 0; i < n; ++i) indeg[i] = 0;\n for (auto e : edges){\n g[e[0]].push_back(e[1]);\n ++indeg[e[1]];\n }\n \n if (!topo()) return -1;\n for (c = 0; c < 26; ++c){\n for (i = 0; i < n; ++i){\n if (C[i] - 'a' == c) q[i] = 1;\n else q[i] = 0;\n f[i] = q[i];\n }\n \n for (int w = 1; w <= n; ++w){\n i = xl[w];\n res = max(res, f[i]);\n for (auto j : g[i]) f[j] = max(f[j], f[i] + q[j]);\n }\n }\n \n return res;\n }\n};", "memory": "129818" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\n\r\npublic:\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n\r\n int n = colors.size();\r\n\r\n vector<vector<int>> graph(n);\r\n\r\n int indegree[n];\r\n\r\n int freq[n][26];\r\n\r\n memset(freq, 0, sizeof(freq));\r\n\r\n memset(indegree, 0, sizeof(indegree));\r\n\r\n for (auto& edge : edges) {\r\n\r\n graph[edge[0]].push_back(edge[1]);\r\n\r\n indegree[edge[1]]++;\r\n\r\n }\r\n\r\n queue<int> q;\r\n\r\n for (int i = 0; i < n; i++) {\r\n\r\n if (indegree[i] == 0) {\r\n\r\n q.push(i);\r\n\r\n }\r\n\r\n }\r\n\r\n int cnt = 0;\r\n\r\n int ans = 0;\r\n\r\n while (!q.empty()) {\r\n\r\n int k = q.size();\r\n\r\n while (k--) {\r\n\r\n int idx = q.front();\r\n\r\n cnt++;\r\n\r\n q.pop();\r\n\r\n freq[idx][colors[idx] - 'a']++;\r\n\r\n ans = max(ans, freq[idx][colors[idx] - 'a']);\r\n\r\n for (auto next : graph[idx]) {\r\n\r\n for (int i = 0; i < 26; i++) {\r\n\r\n freq[next][i] = max(freq[next][i], freq[idx][i]);\r\n\r\n }\r\n\r\n indegree[next]--;\r\n\r\n if (indegree[next] == 0) {\r\n\r\n q.push(next);\r\n\r\n }\r\n\r\n }\r\n\r\n } \r\n\r\n }\r\n\r\n\r\n\r\n if (cnt != n) return -1;\r\n\r\n return ans;\r\n\r\n }\r\n\r\n};", "memory": "129818" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "/*\r\nMistakes:\r\n1. renaming of same variable and func\r\n2. destructuring vector of size 2\r\n\r\na -> b\r\na -> c\r\n\r\n0/1\r\ndp(u):\r\n MAX(color[u] == current_color + dp(v)) for all v in children(u)\r\n\r\nvis[], 0, 1, 2\r\ndp[]\r\n\r\nif vis[0]\r\n run the dp\r\n if vis[1]\r\n then its -1\r\nif vis[2]\r\n return dp[u]\r\n\r\n for all v in children(u):\r\n dp(v)\r\n if -1\r\n return -1;\r\n\r\nE + V;\r\nfor (color = 'a'; color <= 'z'; ++color)\r\n ans = max(ans, get_max_path_sum(color))\r\n\r\n\r\n\r\n*/\r\nclass Solution {\r\nprivate:\r\n int n;\r\n vector<vector<int> > adj;\r\n vector<int> vis;\r\n vector<int> mem;\r\n string colors;\r\n // 0 -> 1 -> 2 -> 3 -> 1\r\n int dp(int u, char color) {\r\n if (vis[u] == 2)\r\n return mem[u];\r\n if (vis[u] == 1)\r\n return -1;\r\n vis[u] = 1;\r\n int &ret = mem[u];\r\n\r\n for (int v : adj[u]) {\r\n int child_ans = dp(v, color);\r\n if (child_ans == -1)\r\n return -1;\r\n ret = max(ret, child_ans);\r\n }\r\n ret += colors[u] == color;\r\n vis[u] = 2;\r\n return ret;\r\n }\r\n\r\n int get_max_path_sum(char color) {\r\n vis.clear();\r\n vis.resize(n);\r\n mem.clear();\r\n mem.resize(n);\r\n\r\n int ans = 0;\r\n for (int u = 0; u < n; ++u) {\r\n int node_ans = dp(u, color);\r\n if (node_ans == -1)\r\n return -1;\r\n ans = max(ans, node_ans);\r\n }\r\n return ans;\r\n }\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n this->n = colors.size();\r\n this->colors = colors;\r\n\r\n adj.resize(n);\r\n for (auto &e: edges) {\r\n adj[e[0]].push_back(e[1]);\r\n }\r\n\r\n int ans = 0;\r\n for (char color = 'a'; color <= 'z'; ++color) {\r\n int color_ans = get_max_path_sum(color);\r\n if (color_ans == -1)\r\n return -1;\r\n ans = max(ans, color_ans);\r\n }\r\n return ans;\r\n }\r\n};", "memory": "131696" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "/*\r\nMistakes:\r\n1. renaming of same variable and func\r\n2. destructuring vector of size 2\r\n\r\na -> b\r\na -> c\r\n\r\n0/1\r\ndp(u):\r\n MAX(color[u] == current_color + dp(v)) for all v in children(u)\r\n\r\nvis[], 0, 1, 2\r\ndp[]\r\n\r\nif vis[0]\r\n run the dp\r\n if vis[1]\r\n then its -1\r\nif vis[2]\r\n return dp[u]\r\n\r\n for all v in children(u):\r\n dp(v)\r\n if -1\r\n return -1;\r\n\r\nE + V;\r\nfor (color = 'a'; color <= 'z'; ++color)\r\n ans = max(ans, get_max_path_sum(color))\r\n\r\n\r\n\r\n*/\r\nclass Solution {\r\nprivate:\r\n int n;\r\n vector<vector<int> > adj;\r\n vector<int> vis;\r\n vector<int> mem;\r\n string colors;\r\n // 0 -> 1 -> 2 -> 3 -> 1\r\n int dp(int u, char color) {\r\n if (vis[u] == 2)\r\n return mem[u];\r\n if (vis[u] == 1)\r\n return -1;\r\n vis[u] = 1;\r\n int &ret = mem[u];\r\n\r\n for (int v : adj[u]) {\r\n int child_ans = dp(v, color);\r\n if (child_ans == -1)\r\n return -1;\r\n ret = max(ret, child_ans);\r\n }\r\n ret += colors[u] == color;\r\n vis[u] = 2;\r\n return ret;\r\n }\r\n\r\n int get_max_path_sum(char color) {\r\n vis.clear();\r\n vis.resize(n);\r\n mem.clear();\r\n mem.resize(n);\r\n\r\n int ans = 0;\r\n for (int u = 0; u < n; ++u) {\r\n int node_ans = dp(u, color);\r\n if (node_ans == -1)\r\n return -1;\r\n ans = max(ans, node_ans);\r\n }\r\n return ans;\r\n }\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n this->n = colors.size();\r\n this->colors = colors;\r\n\r\n adj.resize(n);\r\n for (auto &e: edges) {\r\n adj[e[0]].push_back(e[1]);\r\n }\r\n\r\n int ans = 0;\r\n for (char color = 'a'; color <= 'z'; ++color) {\r\n int color_ans = get_max_path_sum(color);\r\n if (color_ans == -1)\r\n return -1;\r\n ans = max(ans, color_ans);\r\n }\r\n return ans;\r\n }\r\n};", "memory": "131696" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "/*\r\nMistakes:\r\n1. renaming of same variable and func\r\n2. destructuring vector of size 2\r\n\r\na -> b\r\na -> c\r\n\r\n0/1\r\ndp(u):\r\n MAX(color[u] == current_color + dp(v)) for all v in children(u)\r\n\r\nvis[], 0, 1, 2\r\ndp[]\r\n\r\nif vis[0]\r\n run the dp\r\n if vis[1]\r\n then its -1\r\nif vis[2]\r\n return dp[u]\r\n\r\n for all v in children(u):\r\n dp(v)\r\n if -1\r\n return -1;\r\n\r\nE + V;\r\nfor (color = 'a'; color <= 'z'; ++color)\r\n ans = max(ans, get_max_path_sum(color))\r\n\r\n\r\n\r\n*/\r\nclass Solution {\r\nprivate:\r\n int n;\r\n vector<vector<int> > adj;\r\n vector<int> vis;\r\n vector<int> mem;\r\n string colors;\r\n // 0 -> 1 -> 2 -> 3 -> 1\r\n int dp(int u, char color) {\r\n if (vis[u] == 2)\r\n return mem[u];\r\n if (vis[u] == 1)\r\n return -1;\r\n vis[u] = 1;\r\n int &ret = mem[u];\r\n\r\n for (int v : adj[u]) {\r\n int child_ans = dp(v, color);\r\n if (child_ans == -1)\r\n return -1;\r\n ret = max(ret, child_ans);\r\n }\r\n ret += colors[u] == color;\r\n vis[u] = 2;\r\n return ret;\r\n }\r\n\r\n int get_max_path_sum(char color) {\r\n vis.clear();\r\n vis.resize(n);\r\n mem.clear();\r\n mem.resize(n);\r\n\r\n int ans = 0;\r\n for (int u = 0; u < n; ++u) {\r\n int node_ans = dp(u, color);\r\n if (node_ans == -1)\r\n return -1;\r\n ans = max(ans, node_ans);\r\n }\r\n return ans;\r\n }\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n this->n = colors.size();\r\n this->colors = colors;\r\n\r\n adj.resize(n);\r\n for (auto &e: edges) {\r\n adj[e[0]].push_back(e[1]);\r\n }\r\n\r\n int ans = 0;\r\n for (char color = 'a'; color <= 'z'; ++color) {\r\n int color_ans = get_max_path_sum(color);\r\n if (color_ans == -1)\r\n return -1;\r\n ans = max(ans, color_ans);\r\n }\r\n return ans;\r\n }\r\n};", "memory": "133575" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\n int indegree[100000];\r\n vector<int>next[100000];\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int ans = 1;\r\n for (auto edge:edges){\r\n int a = edge[0], b = edge[1];\r\n next[a].push_back(b);\r\n indegree[b]++;\r\n }\r\n unordered_set<char> Set(colors.begin(), colors.end());\r\n for (char ch = 'a'; ch<='z';ch++){\r\n if (Set.find(ch)==Set.end()) continue;\r\n int t = helper(ch-'a', colors, edges);\r\n if (t==-1) return -1;\r\n ans = max(ans, t);\r\n }\r\n return ans;\r\n }\r\n int helper(int k, string colors, vector<vector<int>>& edges) \r\n {\r\n int n = colors.size();\r\n vector<int>count(n, 0);\r\n vector<int>in(n, 0);\r\n for (int i=0; i<n; i++)\r\n in[i] = indegree[i];\r\n \r\n int nodes = 0;\r\n queue<int>q;\r\n for (int i=0; i<n; i++)\r\n {\r\n if (in[i]==0)\r\n {\r\n nodes++;\r\n if (colors[i]-'a'==k) count[i]++;\r\n q.push({i});\r\n }\r\n }\r\n \r\n int ret = 0;\r\n while (!q.empty())\r\n {\r\n auto cur = q.front();\r\n q.pop(); \r\n \r\n for (auto p: next[cur])\r\n {\r\n count[p] = max(count[p], count[cur]+ (colors[p]-'a'==k)); \r\n ret = max(ret, count[p]); \r\n in[p]--;\r\n if (in[p]==0)\r\n { \r\n nodes++;\r\n q.push(p); \r\n }\r\n } \r\n }\r\n \r\n if (nodes!=n) return -1;\r\n return ret;\r\n }\r\n\r\n};", "memory": "133575" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\n int indegree[100000];\r\n vector<int>next[100000];\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int ans = 1;\r\n for (auto edge:edges){\r\n int a = edge[0], b = edge[1];\r\n next[a].push_back(b);\r\n indegree[b]++;\r\n }\r\n unordered_set<char> Set(colors.begin(), colors.end());\r\n for (char ch = 'a'; ch<='z';ch++){\r\n if (Set.find(ch)==Set.end()) continue;\r\n int t = helper(ch-'a', colors, edges);\r\n if (t==-1) return -1;\r\n ans = max(ans, t);\r\n }\r\n return ans;\r\n }\r\n int helper(int k, string colors, vector<vector<int>>& edges){\r\n int n = colors.size();\r\n vector<int> count(n, 0);\r\n vector<int> ind(n, 0);\r\n for (int i = 0; i<n; i++){\r\n ind[i] = indegree[i];\r\n }\r\n queue<int> q;\r\n int nodes = 0;\r\n for (int i = 0; i<n; i++){\r\n if(ind[i]==0){\r\n nodes++;\r\n if (colors[i]-'a'==k) count[i]++;\r\n q.push({i});\r\n }\r\n }\r\n int ret = 0;\r\n while (!q.empty()){\r\n int cur = q.front();\r\n q.pop();\r\n for (auto p:next[cur]){\r\n count[p]=max(count[p], count[cur]+(colors[p]-'a'==k));\r\n ret = max(ret, count[p]);\r\n ind[p]--;\r\n if (ind[p]==0){\r\n nodes++;\r\n q.push(p);\r\n }\r\n }\r\n }\r\n if(nodes!=n) return -1;\r\n return ret;\r\n }\r\n};", "memory": "135454" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n=colors.size();\r\n vector<vector<int>> v(n);\r\n vector<int> ind(n),topo;\r\n for(auto &i:edges){\r\n v[i[0]].push_back(i[1]);\r\n ind[i[1]]++;\r\n }\r\n queue<int> q;\r\n for(int i=0;i<n;i++){\r\n if(ind[i]==0) q.push(i);\r\n }\r\n while(!q.empty()){\r\n int node=q.front();\r\n q.pop();\r\n topo.push_back(node);\r\n for(auto &i:v[node]){\r\n ind[i]--;\r\n if(ind[i]==0)\r\n q.push(i);\r\n }\r\n }\r\n if(topo.size()!=n) return -1;\r\n int ans=0;\r\n for(char c='a';c<='z';c++){\r\n vector<int> dp(n);\r\n for(auto &i:topo){\r\n if(colors[i]==c) dp[i]++;\r\n for(auto &j:v[i])\r\n dp[j]=max(dp[j],dp[i]);\r\n ans=max(dp[i],ans);\r\n }\r\n }\r\n return ans;\r\n }\r\n};", "memory": "135454" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n\r\n\r\n int dp[100005][26];\r\n\r\n bool recStack[100005];\r\n\r\n bool vis[100005];\r\n\r\n void find(int source , vector<int> adj[],string& colors,bool vis[]){\r\n\r\n\r\n vis[source] = true;\r\n\r\n\r\n for(int j = 0;j<adj[source].size();j++){\r\n\r\n int neighbour = adj[source][j];\r\n\r\n if(!vis[neighbour])\r\n find(neighbour,adj,colors,vis);\r\n\r\n for(int i = 0;i<26;i++){\r\n dp[source][i] = max(dp[source][i],dp[neighbour][i]);\r\n }\r\n }\r\n\r\n dp[source][colors[source]-'a']++;\r\n }\r\n\r\n bool hasCycle(vector<int> adj[],int source){\r\n\r\n\r\n vis[source] = true;\r\n recStack[source] = true;\r\n\r\n for(int i=0;i<adj[source].size();i++){\r\n int neighbour = adj[source][i];\r\n\r\n if(recStack[neighbour]) return true;\r\n\r\n if(!vis[neighbour])\r\n {\r\n \r\n bool check = hasCycle(adj , neighbour);\r\n\r\n if(check) return check;\r\n }\r\n }\r\n\r\n recStack[source] = false;\r\n return false;\r\n\r\n } \r\n\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n\r\n\r\n memset(dp,0,sizeof(dp));\r\n memset(vis,false,sizeof(vis));\r\n memset(recStack,false,sizeof(recStack));\r\n\r\n\r\n int n = colors.size();\r\n\r\n vector<int> adj[n];\r\n\r\n\r\n for(int i =0;i<edges.size();i++){\r\n\r\n int from = edges[i][0];\r\n int to = edges[i][1];\r\n\r\n adj[from].push_back(to);\r\n }\r\n \r\n for(int i=0;i<n;i++){\r\n\r\n if(!vis[i]) {\r\n if(hasCycle(adj,i)) return -1;\r\n }\r\n }\r\n\r\n\r\n memset(vis,false,sizeof(vis));\r\n\r\n int ans = 0;\r\n\r\n for(int i=0;i<n;i++){\r\n\r\n if(!vis[i]){\r\n find(i,adj,colors,vis);\r\n }\r\n\r\n for(int j = 0;j<26;j++){\r\n ans = max(ans,dp[i][j]);\r\n }\r\n }\r\n \r\n return ans;\r\n }\r\n};", "memory": "137333" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "\r\n#define pb push_back\r\nclass Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n\r\n int n = colors.length();\r\n\r\n int deg[n];\r\n memset(deg,0,sizeof(deg));\r\n\r\n vector<int>adj[n];\r\n for(auto ele:edges)\r\n {\r\n int u = ele[0];int v = ele[1];\r\n adj[u].pb(v);\r\n deg[v]++;\r\n }\r\n\r\n int cnt[n][26];\r\n memset(cnt,0,sizeof(cnt));\r\n\r\n queue<int>q;\r\n\r\n for(int i=0;i<n;i++)\r\n {\r\n if(deg[i]==0){\r\n q.push(i);\r\n }\r\n cnt[i][colors[i]-'a']++;\r\n }\r\n\r\n int ans = 0; int vis = 0;\r\n\r\n while(!q.empty())\r\n {\r\n int curr = q.front();q.pop();\r\n vis++;\r\n for(auto ele:adj[curr])\r\n {\r\n int cc = colors[ele]-'a';\r\n for(int i=0;i<26;i++)\r\n {\r\n cnt[ele][i] = max(cnt[ele][i] , cnt[curr][i] + (i==cc? 1:0));\r\n }\r\n\r\n deg[ele]--;\r\n if(deg[ele]==0)\r\n {\r\n q.push(ele);\r\n }\r\n }\r\n\r\n for(int i=0;i<26;i++)\r\n {\r\n ans = max(ans,cnt[curr][i]);\r\n }\r\n }\r\n\r\n if(vis!=n)\r\n {\r\n return -1;\r\n }\r\n\r\n return ans;\r\n}\r\n};", "memory": "139211" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n int dp[100005][26]= {}, vis[100005]={},realvis[100005] = {};\r\n vector<int> grp[100005];\r\n bool dfs(int s, string& color){\r\n \r\n bool f = false ;\r\n vis[s] = 1;\r\n realvis[s] = 1;\r\n for(auto x : grp[s]){\r\n if(vis[x] == 1){\r\n return true;\r\n } \r\n if(realvis[x] == 0) {\r\n f |= dfs(x, color);\r\n }\r\n for(int i = 0; i < 26 ; i++)\r\n dp[s][i] = max(dp[s][i], dp[x][i]);\r\n }\r\n dp[s][color[s] - 'a'] += 1;\r\n vis[s] = 0;\r\n return f;\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n const int n = colors.size(), m = edges.size();\r\n vector<int> roots;\r\n vector<int> a(n, 0);\r\n for(int i = 0; i < m ; i++){\r\n a[edges[i][1]] += 1;\r\n if(edges[i][0] == edges[i][1]) return -1;\r\n grp[edges[i][0]].push_back(edges[i][1]);\r\n }\r\n for(int i = 0; i < n; i++){ // path will be from root to any leaf node which contains the largest number of same colors \r\n if(a[i] == 0){\r\n roots.push_back(i);\r\n }\r\n }\r\n if(roots.size() == 0) return -1 ;\r\n int ans = 0;\r\n for(auto x : roots){\r\n bool f = dfs(x, colors);\r\n if(f) return -1;\r\n for(int i = 0; i < 26; i++){\r\n ans = max(ans, dp[x][i]);\r\n }\r\n }\r\n for(int i = 0; i < n; i++){\r\n if(realvis[i] == 0) return -1; // if any of the node is still unvisited the there must be a cycle.\r\n }\r\n return ans ;\r\n }\r\n};", "memory": "141090" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n=colors.size();\r\n vector<int> adj[n];\r\n vector<int> ind(n,0);\r\n for(auto it:edges){\r\n adj[it[0]].push_back(it[1]);\r\n ind[it[1]]++;\r\n }\r\n int dp[n][26];\r\n memset(dp,0,sizeof(dp));\r\n int cnt=0;\r\n queue<int> q;\r\n for(int i=0;i<n;i++){\r\n if(ind[i]==0) q.push(i);\r\n dp[i][colors[i]-'a']++;\r\n }\r\n int maxi=0;\r\n while(!q.empty()){\r\n int node=q.front();\r\n q.pop();\r\n cnt++;\r\n for(auto it:adj[node]){\r\n for(int i=0;i<26;i++){\r\n dp[it][i]=max(dp[it][i],dp[node][i]+(colors[it]-'a' == i));\r\n }\r\n ind[it]--;\r\n if(ind[it]==0) q.push(it);\r\n }\r\n maxi=max(maxi,*max_element(dp[node],dp[node]+26));\r\n }\r\n if(cnt==n) return maxi;\r\n return -1; \r\n }\r\n};", "memory": "141090" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n int dp[100001][26];\r\n vector<int> outgoing[100001];\r\n\r\n int dfs(int start, vector <int> &vis, string &s) {\r\n \r\n // if(vis[start])\r\n // return -1;\r\n\r\n if(dp[start][0] != -1)\r\n return 0;\r\n\r\n for(int i=0;i<outgoing[start].size();i++) {\r\n int ind = outgoing[start][i];\r\n\r\n if(vis[ind])\r\n return -1;\r\n\r\n vis[ind] = 1;\r\n\r\n cout<<start<<\" \"<<ind<<\"\\n\";\r\n\r\n if(dp[ind][0] == -1) {\r\n \r\n int val = dfs(ind, vis, s);\r\n\r\n cout<<val<<\" \"<<ind<<\"A\\n\";\r\n\r\n if(val == -1)\r\n return -1;\r\n }\r\n\r\n for(int j=0;j<26;j++) {\r\n if(j == s[start] - 'a') {\r\n dp[start][j] = max(dp[start][j], 1+dp[ind][j]);\r\n continue;\r\n }\r\n\r\n dp[start][j] = max(dp[start][j], dp[ind][j]);\r\n }\r\n\r\n vis[ind] = 0;\r\n }\r\n\r\n if(outgoing[start].size() == 0) {\r\n for(int i=0;i<26;i++)\r\n dp[start][i] = 0;\r\n\r\n dp[start][s[start] - 'a'] = 1;\r\n }\r\n\r\n return 0;\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n\r\n int n = colors.size();\r\n \r\n for(int i=0;i<edges.size();i++) {\r\n outgoing[edges[i][0]].push_back(edges[i][1]);\r\n\r\n //self edge\r\n if(edges[i][0] == edges[i][1])\r\n return -1;\r\n }\r\n\r\n vector<int> vis(n, 0);\r\n\r\n for(int i=0;i<=colors.size();i++)\r\n for(int j=0;j<26;j++)\r\n dp[i][j] = -1;\r\n\r\n for(int i=0;i<n;i++) {\r\n cout<<\"start \"<<i<<\"\\n\";\r\n vis[i] = 1;\r\n int val = dfs(i, vis, colors);\r\n vis[i] = 0;\r\n\r\n if(val == -1)\r\n return -1;\r\n }\r\n\r\n int ans = 0;\r\n\r\n for(int i=0;i<=colors.size();i++)\r\n for(int j=0;j<26;j++)\r\n ans = max(ans, dp[i][j]);\r\n\r\n return ans;\r\n }\r\n};", "memory": "142969" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\n private: static const int N = 1e5 + 5;\r\npublic:\r\n\r\nint indegree[N];\r\nint dp[N][26];\r\nint vis[N] , anc[N];\r\nvector<int>gr[N];\r\n// int dfs(int src , int par){\r\n// vis[src] = 1;\r\n// for(auto i : gr[src]){\r\n// if (anc[i]) return false;\r\n// if (vis[i]) continue;\r\n// anc[i] = 1;\r\n// int val = dfs(i , src);\r\n// anc[i] = 0;\r\n// if (!val) return false;\r\n// }\r\n// return true;\r\n// }\r\nint largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n vector<int>v;\r\n for(int i = 0 ; i<n ; i++) v.push_back(colors[i] - 'a');\r\n for(auto i : edges){\r\n gr[i[0]].push_back(i[1]);\r\n indegree[i[1]] ++;\r\n }\r\n // for(int i = 0 ; i<n ; i++){\r\n // if (!vis[i]) {\r\n // anc[i] = 1;\r\n // int val = dfs(i , -1);\r\n // if (!val) return -1;\r\n // }\r\n // }\r\n int cnt = 0;\r\n queue<int>q;\r\n for(int i = 0 ; i<n ; i++){\r\n if (!indegree[i]) q.push(i);\r\n }\r\n int ans = 0;\r\n while(!q.empty()){\r\n int top = q.front();\r\n q.pop();\r\n cnt ++;\r\n // cout<<v[top]<<\" \";\r\n dp[top][v[top]] ++;\r\n ans = max(ans , dp[top][v[top]]);\r\n for(auto i : gr[top]){\r\n indegree[i] --;\r\n for(int j = 0 ; j<26 ; j++){\r\n dp[i][j] = max(dp[i][j] , dp[top][j]);\r\n }\r\n if (indegree[i] == 0) q.push(i);\r\n }\r\n }\r\n return (cnt == n?ans:-1);\r\n}\r\n};", "memory": "142969" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n\r\nint mx = 0;\r\nint dp[100001][26]; \r\n\r\nvector<int> toposort(int n, vector<int> g[]) {\r\n vector<int> indeg(n, 0);\r\n for(int i = 0; i < n; i++) {\r\n for(auto& b: g[i]) {\r\n indeg[b]++;\r\n }\r\n }\r\n\r\n queue<int> q;\r\n for(int i = 0; i < n; i++) {\r\n if(indeg[i] == 0) q.push(i);\r\n }\r\n vector<int> topo;\r\n while(!q.empty()) {\r\n int node = q.front(); q.pop();\r\n topo.push_back(node);\r\n for(auto& b: g[node]) {\r\n indeg[b]--;\r\n if(indeg[b] == 0) q.push(b);\r\n }\r\n }\r\n return topo;\r\n}\r\n\r\nvoid dfs(int node, vector<int>& vis, string& col, vector<int> g[]) {\r\n vis[node] = 1;\r\n for(auto& b: g[node]) {\r\n if(!vis[b]) dfs(b, vis, col, g);\r\n for(int i = 0; i < 26; i++) dp[node][i] = max(dp[node][i], dp[b][i]);\r\n } \r\n dp[node][col[node] - 'a']++;\r\n for(int i = 0; i < 26; i++) mx = max(mx, dp[node][i]);\r\n}\r\n int largestPathValue(string col, vector<vector<int>>& e) {\r\n int n = col.size();\r\n // rev graph\r\n vector<int> g[n];\r\n for(auto& it: e) {\r\n int a = it[0], b = it[1];\r\n g[a].push_back(b);\r\n }\r\n vector<int> topo = toposort(n, g);\r\n if(topo.size() != n) return -1;\r\n vector<int> vis(n, 0);\r\n for(int i = 0; i < n; i++) {\r\n if(!vis[topo[i]]) dfs(topo[i], vis, col, g);\r\n }\r\n\r\n return mx;\r\n }\r\n};", "memory": "144848" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int dp[100001][26];\n bool detectCycle = false;\n int maxColor = 1;\n void getLargestValue(int node,vector<int>&dfsVisited,vector<vector<int>>&adjacencyList,string &colors){\n if(dp[node][colors[node]-'a']!=-1) return ;\n \n dp[node][colors[node]-'a']=1;\n dfsVisited[node] = true;\n\n for(int ele:adjacencyList[node]){\n if(dfsVisited[ele]){\n detectCycle = true;\n return;\n } \n \n getLargestValue(ele,dfsVisited,adjacencyList,colors);\n \n if(detectCycle) {\n return;\n }\n \n for(int i=0;i<26;i++){\n if(colors[node]-'a'== i){\n dp[node][i] = max(dp[node][i],1+dp[ele][i]);\n } else {\n dp[node][i] = max(dp[node][i],dp[ele][i]);\n }\n maxColor = max(dp[node][i],maxColor);\n }\n }\n \n dfsVisited[node] = false;\n \n }\n \n int largestPathValue(string colors, vector<vector<int>>& edges) {\n memset(dp,-1,sizeof(dp));\n vector<vector<int>>adjacencyList(colors.size());\n \n for(int i=0;i<edges.size();i++){\n adjacencyList[edges[i][0]].push_back(edges[i][1]);\n }\n \n // once a path is started if dfs visited is there it will reach that path inevitably\n \n // we are caching items for some reasn?? -> so when we go to the node we don't want to iterate through the whole node\n vector<int>dfsVisited(colors.size(),false);\n for(int i=0;i<colors.size();i++){\n getLargestValue(i,dfsVisited,adjacencyList,colors);\n if(detectCycle) return -1;\n\n }\n \n\n return maxColor;\n }\n};", "memory": "144848" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n int solve(int color, string & s, vector<int> adj[], int n, int node, vector<int> & dp)\r\n {\r\n if(dp[node]!=-1)\r\n return dp[node];\r\n\r\n int ans=0;\r\n for(auto i: adj[node])\r\n {\r\n ans= max(ans, solve(color, s, adj, n, i, dp));\r\n }\r\n\r\n dp[node]= ans;\r\n\r\n if(color== (s[node]-'a'))\r\n dp[node]++;\r\n\r\n return dp[node];\r\n }\r\n\r\n \r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n \r\n int n= colors.size();\r\n vector<int> degree(n, 0);\r\n \r\n vector<int> adj[n];\r\n\r\n for(int i=0; i<edges.size(); i++)\r\n {\r\n int u= edges[i][0];\r\n int v= edges[i][1];\r\n //swap(u, v);\r\n adj[u].push_back(v);\r\n degree[v]++;\r\n }\r\n queue<int> q;\r\n vector<int> topo;\r\n for(int i=0; i<n; i++)\r\n {\r\n if(degree[i]==0)\r\n q.push(i);\r\n }\r\n\r\n while(!q.empty())\r\n {\r\n int node= q.front();\r\n q.pop();\r\n\r\n topo.push_back(node);\r\n for(auto i: adj[node])\r\n {\r\n degree[i]--;\r\n if(degree[i]==0)\r\n q.push(i);\r\n }\r\n }\r\n\r\n if(topo.size()< n)\r\n return -1;\r\n \r\n int ans=0;\r\n for(int color=0; color<26; color++)\r\n {\r\n vector<int> dp(n, -1);\r\n for(int i=0; i<topo.size(); i++)\r\n {\r\n if(dp[topo[i]]==-1)\r\n {\r\n ans= max(ans, solve(color, colors, adj, n, topo[i], dp));\r\n }\r\n }\r\n\r\n }\r\n\r\n return ans;\r\n\r\n\r\n\r\n\r\n \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n \r\n }\r\n};", "memory": "146726" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int largestPathValue(string colors, vector<vector<int>>& edges) {\n // creating graph\n int n = colors.size();\n vector<vector<int>> adj = vector(n, vector<int>());\n vector<int> in(n, 0);\n for(auto &v : edges){\n adj[v[0]].push_back(v[1]);\n in[v[1]]++;\n }\n \n // traversal in topological ordering\n vector<array<int, 26>> fr(n);\n queue<int> q;\n for(int i = 0; i < n; i++){\n if(in[i] == 0){\n q.push(i);\n fr[i][colors[i] - 'a']++;\n }\n }\n int res = 0, seen = 0;;\n while(q.size()){\n int sz = q.size();\n for(int i = 0; i < sz; i++){\n int u = q.front();\n q.pop();\n res = max(res, *max_element(fr[u].begin(), fr[u].end()));\n seen++;\n for(int v : adj[u]){\n for(int j = 0; j < 26; j++){\n fr[v][j] = max(fr[v][j], fr[u][j] + (j == (colors[v] - 'a')));\n }\n in[v]--;\n if(in[v] == 0){\n q.push(v);\n }\n }\n }\n }\n if(seen < n) return -1;\n return res;\n }\n};", "memory": "146726" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n=colors.size();\r\n int m=edges.size();\r\n vector<int> adj[n];\r\n vector<int> indeg(n);\r\n for(int i=0;i<m;i++)\r\n {\r\n int u=edges[i][0];\r\n int v=edges[i][1];\r\n adj[u].push_back(v);\r\n indeg[v]++;\r\n }\r\n queue<int> q;\r\n vector<int> toposort;\r\n for(int i=0;i<n;i++)\r\n {\r\n if(indeg[i]==0)\r\n q.push(i);\r\n }\r\n while(!q.empty())\r\n {\r\n auto node=q.front();\r\n q.pop();\r\n toposort.push_back(node);\r\n for(auto it:adj[node])\r\n {\r\n if(indeg[it]>0)\r\n {\r\n indeg[it]--;\r\n if(indeg[it]==0)\r\n q.push(it);\r\n }\r\n }\r\n }\r\n if(toposort.size()!=n)\r\n return -1;\r\n \r\n vector<vector<int>> dp(n,vector<int>(26));\r\n int ans=0;\r\n for(int i=0;i<n;i++)\r\n {\r\n cout<<toposort[i]<<\" \";\r\n }\r\n cout<<endl;\r\n for(int i=n-1;i>=0;i--)\r\n {\r\n int node=toposort[i];\r\n \r\n // char ch=colors[node];\r\n for(char ch='a';ch<='z';ch++)\r\n {\r\n int cnt=0;\r\n for(auto it:adj[node])\r\n {\r\n cnt=max(cnt,dp[it][ch-'a']);\r\n }\r\n dp[node][ch-'a']=cnt;\r\n ans=max(ans,dp[node][ch-'a']);\r\n }\r\n dp[node][colors[node]-'a']+=1;\r\n ans=max(ans, dp[node][colors[node]-'a']);\r\n }\r\n return ans;\r\n }\r\n};", "memory": "148605" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n_nodes = colors.size();\r\n vector<vector<int>> adj(n_nodes); // adjacency list\r\n vector<int> indegree(n_nodes, 0); // indegree array\r\n\r\n // Construct the adjacency list and fill the indegree array\r\n for (const auto& edge : edges) {\r\n adj[edge[0]].push_back(edge[1]);\r\n indegree[edge[1]]++;\r\n }\r\n\r\n queue<int> topo;\r\n stack<int> s;\r\n\r\n // Add all nodes with 0 indegree to the queue\r\n for (int i = 0; i < n_nodes; i++) {\r\n if (indegree[i] == 0) {\r\n topo.push(i);\r\n s.push(i);\r\n }\r\n }\r\n\r\n int visited_nodes = 0; // to detect cycles\r\n\r\n while (!topo.empty()) {\r\n int node = topo.front();\r\n topo.pop();\r\n visited_nodes++;\r\n\r\n for (int neighbor : adj[node]) {\r\n indegree[neighbor]--;\r\n if (indegree[neighbor] == 0) {\r\n topo.push(neighbor);\r\n s.push(neighbor);\r\n }\r\n }\r\n }\r\n\r\n // If visited nodes count is less than n_nodes, there is a cycle\r\n if (visited_nodes < n_nodes) {\r\n return -1;\r\n }\r\n\r\n vector<vector<int>> dp(n_nodes, vector<int>(26, 0));\r\n int maxf = 0;\r\n\r\n while (!s.empty()) {\r\n int node = s.top();\r\n s.pop();\r\n int maxi = 0;\r\n\r\n for (int i = 0; i < 26; i++) {\r\n int maxc = 0;\r\n for (int neighbor : adj[node]) {\r\n maxc = max(maxc, dp[neighbor][i]);\r\n }\r\n if (i == colors[node] - 'a') {\r\n dp[node][i] = 1 + maxc;\r\n } else {\r\n dp[node][i] = maxc;\r\n }\r\n maxi = max(maxi, dp[node][i]);\r\n }\r\n maxf = max(maxf, maxi);\r\n }\r\n\r\n return maxf;\r\n\r\n }\r\n};", "memory": "148605" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n // detect there is a cycle\r\n // crate a 2d array m x 26\r\n int n = colors.size();\r\n vector<int> indegree(n, 0);\r\n vector<int> outdegree(n, 0);\r\n int arr[100005][26];\r\n for (int i = 0; i < n; i++) {\r\n fill(arr[i], arr[i] + 26, 0);\r\n }\r\n \r\n vector<vector<int>> adj(n);\r\n vector<vector<int>> rev_adj(n);\r\n\r\n for (int i = 0; i < edges.size(); i++) {\r\n int first_node = edges[i][0];\r\n int second_node = edges[i][1];\r\n indegree[second_node] ++;\r\n outdegree[first_node] ++;\r\n adj[first_node].push_back(second_node);\r\n rev_adj[second_node].push_back(first_node);\r\n }\r\n\r\n queue<int> topological_queue;\r\n \r\n for (int i = 0; i < n; i++) {\r\n if (indegree[i] == 0) {\r\n topological_queue.push(i);\r\n }\r\n }\r\n while (!topological_queue.empty()) {\r\n int cur_node = topological_queue.front();\r\n topological_queue.pop();\r\n for (int i = 0; i < adj[cur_node].size(); i++) {\r\n int nxt_node = adj[cur_node][i];\r\n indegree[nxt_node] --;\r\n if (indegree[nxt_node] == 0) {\r\n topological_queue.push(nxt_node);\r\n }\r\n }\r\n }\r\n for (int i = 0; i < n; i++) {\r\n if (indegree[i] > 0) {\r\n return -1;\r\n }\r\n }\r\n\r\n for (int i = 0; i < n; i++) {\r\n if (outdegree[i] == 0) {\r\n topological_queue.push(i);\r\n int color_index = colors[i] - 'a';\r\n arr[i][color_index] = 1;\r\n }\r\n }\r\n\r\n while (!topological_queue.empty()) {\r\n int cur_node = topological_queue.front();\r\n topological_queue.pop();\r\n for (int i = 0; i < rev_adj[cur_node].size(); i++) {\r\n int nxt_node = rev_adj[cur_node][i];\r\n int nxt_color = colors[nxt_node] - 'a';\r\n for (int j = 0; j < 26; j++) {\r\n if (j == nxt_color) {\r\n arr[nxt_node][j] = max(arr[nxt_node][j], arr[cur_node][j] + 1);\r\n }\r\n arr[nxt_node][j] = max(arr[nxt_node][j], arr[cur_node][j]);\r\n }\r\n outdegree[nxt_node] --;\r\n if (outdegree[nxt_node] == 0) {\r\n topological_queue.push(nxt_node);\r\n }\r\n }\r\n\r\n }\r\n int max_ans = 0;\r\n for (int i = 0; i < n; i++) {\r\n for (int j = 0; j < 26; j++) {\r\n max_ans = max(max_ans, arr[i][j]);\r\n }\r\n }\r\n return max_ans;\r\n \r\n }\r\n};", "memory": "150484" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size(), m = edges.size();\r\n vector<vector<int>> g(n);\r\n vector<int> d(n); // d[i]表示i点的入度\r\n for (auto& e: edges) {\r\n int a = e[0], b = e[1];\r\n g[a].push_back(b);\r\n d[b] ++;\r\n }\r\n\r\n // 用拓扑排序来判断图中是否有环\r\n vector<int> p; // 存储bfs的递推序列\r\n queue<int> q;\r\n for (int i = 0; i < n; i ++)\r\n if (!d[i]) q.push(i); // 入度为0,入队\r\n while (q.size()) {\r\n auto t = q.front();\r\n q.pop();\r\n p.push_back(t);\r\n for (int v: g[t]) {\r\n if (! -- d[v]) q.push(v); // 若删去t点后,v点入度为0,则入队\r\n }\r\n }\r\n if (p.size() < n) return -1; // 有环\r\n \r\n vector<vector<int>> f(n, vector<int>(26)); // f[i][j]表示以i为终点的所有路径中,包含颜色j的数量的最大值\r\n int res = 0;\r\n for (int i: p) {\r\n int c = colors[i] - 'a'; // c为i点的颜色\r\n f[i][c] = max(f[i][c], 1); // f[i][c]为以i为终点的路径中,包含颜色c的最多数量,至少也有1个\r\n \r\n // 枚举26种颜色j\r\n for (int j = 0; j < 26; j ++) {\r\n // 枚举i的每一个邻点k\r\n for (int k: g[i]) {\r\n int t = 0;\r\n if (colors[k] - 'a' == j) t = 1; // k点的颜色是j,则t = 1\r\n // 根据f[i][j]递推f[k][j]\r\n f[k][j] = max(f[k][j], f[i][j] + t);\r\n }\r\n res = max(res, f[i][j]);\r\n }\r\n\r\n }\r\n return res;\r\n }\r\n};\r\n\r\n/*\r\nn个点,m条边,有向图。\r\n返回图中最长路径中的最多元素。\r\n\r\n首先,判断图中是否有环,若有环,直接返回-1;\r\n然后,枚举26种颜色,计算每条路径上包含该颜色的最多数量是多少;(最长路问题)\r\n\r\nf[i]表示以i为终点的所有路径中,包含颜色a的数量的最大值是多少。\r\n对于所有可以到达j的i,则f[j] = max(f[j], f[i] + t), 若j的颜色是a,则t = 1, 否则t = 0。\r\n*/", "memory": "152363" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size(), m = edges.size();\r\n vector<vector<int>> g(n);\r\n vector<int> d(n); // d[i]表示i点的入度\r\n for (auto& e: edges) {\r\n int a = e[0], b = e[1];\r\n g[a].push_back(b);\r\n d[b] ++;\r\n }\r\n\r\n // 用拓扑排序来判断图中是否有环\r\n vector<int> p; // 存储bfs的递推序列\r\n queue<int> q;\r\n for (int i = 0; i < n; i ++)\r\n if (!d[i]) q.push(i); // 入度为0,入队\r\n while (q.size()) {\r\n auto t = q.front();\r\n q.pop();\r\n p.push_back(t);\r\n for (int v: g[t]) {\r\n if (! -- d[v]) q.push(v); // 若删去t点后,v点入度为0,则入队\r\n }\r\n }\r\n if (p.size() < n) return -1; // 有环\r\n\r\n vector<vector<int>> f(n, vector<int>(26)); // f[i][j]表示以i为终点的所有路径中,包含颜色j的数量的最大值\r\n int res = 0;\r\n for (int i: p) {\r\n int c = colors[i] - 'a'; // c为i点的颜色\r\n f[i][c] = max(f[i][c], 1); // f[i][c]为以i为终点的路径中,包含颜色c的最多数量,至少也有1个\r\n \r\n // 枚举26种颜色j\r\n for (int j = 0; j < 26; j ++) {\r\n // 枚举i的每一个邻点k\r\n for (int k: g[i]) {\r\n int t = 0;\r\n if (colors[k] - 'a' == j) t = 1; // k点的颜色是j,则t = 1\r\n // 根据f[i][j]递推f[k][j]\r\n f[k][j] = max(f[k][j], f[i][j] + t);\r\n }\r\n res = max(res, f[i][j]);\r\n }\r\n }\r\n return res;\r\n }\r\n};\r\n\r\n/*\r\n首先,判断图中是否有环,若有环,直接返回-1;\r\n然后,枚举26种颜色,计算每条路径上包含该颜色的最多数量是多少;(拓扑图的最长路问题)\r\n\r\nf[i][j]表示以i为终点的所有路径中,包含颜色j的数量的最大值是多少。\r\n对于所有可以到达k的i,则f[k][j] = max(f[k][j], f[i][j] + t), 若k点的颜色是j,则t = 1, 否则t = 0。\r\n*/", "memory": "152363" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n int dp[100100][26];\r\n void dfs(int node,vector<int>adj[],vector<int>&vis,string &s){\r\n vis[node]=1;\r\n for(auto it:adj[node]){\r\n if(!vis[it]){\r\n dfs(it,adj,vis,s);\r\n }\r\n for(int j=0;j<26;j++){\r\n dp[node][j]=max(dp[it][j],dp[node][j]);\r\n }\r\n }\r\n dp[node][s[node]-'a']++;\r\n }\r\n bool containsCycle(int node,vector<int>adj[],vector<int>&vis,vector<int>&path){\r\n vis[node]=1;\r\n path[node]=1;\r\n for(auto it:adj[node]){\r\n if(!vis[it]){\r\n if(containsCycle(it,adj,vis,path))return true;\r\n }else{\r\n if(path[it])return true;\r\n }\r\n }\r\n path[node]=0;\r\n return false;\r\n }\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n vector<int>adj[n],in(n);\r\n for(auto it:edges){\r\n adj[it[0]].push_back(it[1]);\r\n in[it[1]]++;\r\n }\r\n\r\n vector<int>vis(n),path(n),c(26);\r\n for(int i=0;i<n;i++){\r\n if(vis[i])continue;\r\n if(containsCycle(i,adj,vis,path)){\r\n return -1;\r\n }\r\n }\r\n vis.assign(n,0);\r\n int ans=0;\r\n memset(dp,0,sizeof(dp));\r\n for(int i=0;i<n;i++){\r\n if(!vis[i])dfs(i,adj,vis,colors);\r\n for(int j=0;j<26;j++){\r\n ans=max(ans,dp[i][j]);\r\n }\r\n }\r\n return ans;\r\n }\r\n};", "memory": "154241" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n void dfs(int node, vector<int> list[], vector<int>& vis, vector<vector<int>>& m, vector<int>& val){\r\n vis[node] = 1;\r\n for(auto it : list[node]){\r\n if(vis[it]==0) dfs(it, list, vis, m, val);\r\n for(int i = 0;i<26;i++){\r\n m[node][i] = max(m[node][i], m[it][i]);\r\n }\r\n }\r\n m[node][val[node]]++;\r\n return;\r\n }\r\n int largestPathValue(string str, vector<vector<int>>& edg) {\r\n int n = str.size();\r\n int e = edg.size();\r\n vector<int> indeg(n);\r\n vector<int> list[n];\r\n for(int i = 0;i<e;i++){\r\n list[edg[i][0]].push_back(edg[i][1]);\r\n indeg[edg[i][1]]++;\r\n }\r\n queue<int> q;\r\n vector<int> vis(n);\r\n for(int i = 0;i<n;i++){\r\n if(indeg[i]==0){\r\n q.push(i);\r\n vis[i] = 1;\r\n }\r\n }\r\n while(q.size()>0){\r\n int node = q.front();\r\n q.pop();\r\n for(auto it : list[node]){\r\n indeg[it]--;\r\n if(indeg[it]==0){\r\n q.push(it);\r\n vis[it] = 1;\r\n }\r\n }\r\n }\r\n for(int i = 0;i<n;i++){\r\n if(vis[i]==0) return -1;\r\n }\r\n vector<vector<int>> m(n, vector<int>(26, 0));\r\n vector<int> val(n);\r\n for(int i = 0;i<n;i++) val[i] = str[i]-'a';\r\n vector<int> v2(n);\r\n for(int i = 0;i<n;i++){\r\n if(v2[i]==0) dfs(i, list, v2, m, val);\r\n }\r\n int ans = 0;\r\n for(int i = 0;i<n;i++){\r\n for(auto it : m[i]) ans = max(ans, it);\r\n }\r\n // cout<<m[3][0]<<endl;\r\n return ans;\r\n\r\n\r\n }\r\n};", "memory": "154241" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n\r\n void dfs(int u, vector<vector<int>> &adj, vector<vector<int>> &dp, string &colors){\r\n if(dp[u][0] != -1)return;\r\n\r\n for(int i=0;i<26;i++){\r\n dp[u][i] = 0;\r\n }\r\n\r\n // dp[u][colors[u] - 'a']++;\r\n for(auto ne : adj[u]){\r\n if(dp[ne][0] == -1){\r\n dfs(ne, adj, dp, colors);\r\n }\r\n\r\n for(int i=0;i<26;i++){\r\n dp[u][i] = max(dp[u][i], dp[ne][i]);\r\n }\r\n }\r\n\r\n dp[u][colors[u] - 'a']++;\r\n }\r\n\r\n bool isCycle(vector<vector<int>> &adj, vector<int> &in, int n){\r\n queue<int> q;\r\n for(int i=0;i<n;i++){\r\n if(in[i] == 0){\r\n q.push(i);\r\n }\r\n }\r\n int cnt = 0;\r\n while(!q.empty()){\r\n // ans.push_back(q.front());\r\n int u = q.front();\r\n cnt++;\r\n q.pop();\r\n for(auto ne : adj[u]){\r\n in[ne]--;\r\n if(in[ne] == 0){\r\n q.push(ne);\r\n }\r\n }\r\n }\r\n\r\n return cnt != n;\r\n }\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n // dp[u][c] -> path starting at u and color = c ka count\r\n // dp[u][c] = max(dp[ne][c]) + 1 whichever c is that node\r\n int n = colors.size();\r\n\r\n vector<vector<int>> adj(n);\r\n vector<int> in(n, 0);\r\n\r\n for(int i=0;i<edges.size();i++){\r\n int u = edges[i][0];\r\n int v = edges[i][1];\r\n\r\n adj[u].push_back(v);\r\n in[v]++;\r\n }\r\n\r\n if(isCycle(adj, in, n))return -1;\r\n in.clear();\r\n int ans = 0;\r\n\r\n vector<vector<int>> dp(n , vector<int> (26, -1));\r\n\r\n for(int i=0;i<n;i++){\r\n if(dp[i][0] == -1){\r\n dfs(i, adj, dp, colors);\r\n }\r\n // if(dp[i][0] == INT_MAX)return -1;\r\n for(int j=0;j<26;j++){\r\n if(dp[i][j] < 0)return -1;\r\n ans = max(ans, dp[i][j]);\r\n }\r\n }\r\n\r\n return ans;\r\n }\r\n};", "memory": "156120" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n\r\n bool dfsCheckCycle(int node, vector<int> adj[], vector<int>& visited, vector<int>& recStack) {\r\n visited[node] = 1; // Mark node as visited\r\n recStack[node] = 1; // Mark node as part of the recursion stack\r\n\r\n // Visit all neighbors\r\n for (int neighbor : adj[node]) {\r\n // If neighbor is not visited, recurse on it\r\n if (!visited[neighbor]) {\r\n if (dfsCheckCycle(neighbor, adj, visited, recStack)) {\r\n return true; // Cycle found\r\n }\r\n }\r\n // If neighbor is already in the recursion stack, we found a cycle\r\n else if (recStack[neighbor]) {\r\n return true; // Cycle found\r\n }\r\n }\r\n\r\n recStack[node] = 0; // Remove the node from the recursion stack after processing\r\n return false;\r\n }\r\n\r\n // Function to check if a cycle exists in the graph\r\n bool isCyclic(vector<int> adj[], int n) {\r\n vector<int> visited(n, 0); // Visited array\r\n vector<int> recStack(n, 0); // Recursion stack\r\n\r\n // Perform DFS for every unvisited node\r\n for (int i = 0; i < n; ++i) {\r\n if (!visited[i]) {\r\n if (dfsCheckCycle(i, adj, visited, recStack)) {\r\n return true; // Cycle detected\r\n }\r\n }\r\n }\r\n\r\n return false; // No cycle found\r\n }\r\n\r\n void dfs(int node, vector<int> adj[], vector<int>& visited, stack<int>& topoStack) {\r\n visited[node] = 1; // Mark the node as visited\r\n\r\n // Visit all neighbors\r\n for (int neighbor : adj[node]) {\r\n if (!visited[neighbor]) {\r\n dfs(neighbor, adj, visited, topoStack);\r\n }\r\n }\r\n\r\n // After all neighbors are processed, push the current node to the stack\r\n topoStack.push(node);\r\n }\r\n\r\n void dfs2(int node, vector<int> adj[], vector<int>& visited, string &colors, vector<vector<int>> &dp){\r\n visited[node]=1;\r\n int curr_color = colors[node]-'a';\r\n dp[node][curr_color] = 1;\r\n for(int i=0;i<27;i++){\r\n for(int j=0;j<adj[node].size();j++){\r\n \r\n int neighbour = adj[node][j];\r\n // cout<<node<<\" \"<<neighbour<<endl;\r\n int temp = 0;\r\n if(i==curr_color) temp=1;\r\n if(visited[neighbour]==0) dfs2(neighbour, adj, visited, colors, dp);\r\n dp[node][i] = max(dp[neighbour][i] +temp, dp[node][i]);\r\n }\r\n }\r\n return;\r\n }\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n vector<int> adj[colors.size()];\r\n vector<int> visited(colors.size(),0);\r\n for (const auto& edge : edges) {\r\n int u = edge[0]; // from node\r\n int v = edge[1]; // to node\r\n adj[u].push_back(v);\r\n // adj[v].push_back(u);\r\n }\r\n if(isCyclic(adj, colors.size())) return -1;\r\n stack<int> topoStack; // Stack to store the topological sort\r\n\r\n // Perform DFS on all nodes\r\n for (int i = 0; i < colors.size(); ++i) {\r\n if (!visited[i]) {\r\n dfs(i, adj, visited, topoStack);\r\n }\r\n }\r\n\r\n // Create a vector to store the result in topological order\r\n vector<int> topoOrder;\r\n while (!topoStack.empty()) {\r\n topoOrder.push_back(topoStack.top());\r\n // cout<<topoStack.top()<<endl;\r\n topoStack.pop();\r\n }\r\n vector<vector<int>> dp(colors.size(),vector<int>(27,0));\r\n vector<int> visited2(colors.size(),0);\r\n int ans=1;\r\n for(int i=0;i<topoOrder.size();i++){\r\n \r\n if(visited2[i]==0){\r\n // cout<<topoOrder[i]<<endl;\r\n dfs2(i, adj, visited2, colors, dp);\r\n \r\n for(int j=0;j<27;j++){\r\n ans=max(ans, dp[i][j]);\r\n }\r\n }\r\n }\r\n return ans;\r\n\r\n }\r\n};", "memory": "165514" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n\r\n bool dfsCheckCycle(int node, vector<int> adj[], vector<int>& visited, vector<int>& recStack) {\r\n visited[node] = 1; // Mark node as visited\r\n recStack[node] = 1; // Mark node as part of the recursion stack\r\n\r\n // Visit all neighbors\r\n for (int neighbor : adj[node]) {\r\n // If neighbor is not visited, recurse on it\r\n if (!visited[neighbor]) {\r\n if (dfsCheckCycle(neighbor, adj, visited, recStack)) {\r\n return true; // Cycle found\r\n }\r\n }\r\n // If neighbor is already in the recursion stack, we found a cycle\r\n else if (recStack[neighbor]) {\r\n return true; // Cycle found\r\n }\r\n }\r\n\r\n recStack[node] = 0; // Remove the node from the recursion stack after processing\r\n return false;\r\n }\r\n\r\n // Function to check if a cycle exists in the graph\r\n bool isCyclic(vector<int> adj[], int n) {\r\n vector<int> visited(n, 0); // Visited array\r\n vector<int> recStack(n, 0); // Recursion stack\r\n\r\n // Perform DFS for every unvisited node\r\n for (int i = 0; i < n; ++i) {\r\n if (!visited[i]) {\r\n if (dfsCheckCycle(i, adj, visited, recStack)) {\r\n return true; // Cycle detected\r\n }\r\n }\r\n }\r\n\r\n return false; // No cycle found\r\n }\r\n\r\n void dfs(int node, vector<int> adj[], vector<int>& visited, stack<int>& topoStack) {\r\n visited[node] = 1; // Mark the node as visited\r\n\r\n // Visit all neighbors\r\n for (int neighbor : adj[node]) {\r\n if (!visited[neighbor]) {\r\n dfs(neighbor, adj, visited, topoStack);\r\n }\r\n }\r\n\r\n // After all neighbors are processed, push the current node to the stack\r\n topoStack.push(node);\r\n }\r\n\r\n void dfs2(int node, vector<int> adj[], vector<int>& visited, string &colors, vector<vector<int>> &dp){\r\n visited[node]=1;\r\n int curr_color = colors[node]-'a';\r\n dp[node][curr_color] = 1;\r\n for(int i=0;i<26;i++){\r\n for(int j=0;j<adj[node].size();j++){\r\n \r\n int neighbour = adj[node][j];\r\n // cout<<node<<\" \"<<neighbour<<endl;\r\n int temp = 0;\r\n if(i==curr_color) temp=1;\r\n if(visited[neighbour]==0) dfs2(neighbour, adj, visited, colors, dp);\r\n dp[node][i] = max(dp[neighbour][i] +temp, dp[node][i]);\r\n }\r\n }\r\n return;\r\n }\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n vector<int> adj[colors.size()];\r\n vector<int> visited(colors.size(),0);\r\n for (const auto& edge : edges) {\r\n int u = edge[0]; // from node\r\n int v = edge[1]; // to node\r\n adj[u].push_back(v);\r\n // adj[v].push_back(u);\r\n }\r\n if(isCyclic(adj, colors.size())) return -1;\r\n stack<int> topoStack; // Stack to store the topological sort\r\n\r\n // Perform DFS on all nodes\r\n for (int i = 0; i < colors.size(); ++i) {\r\n if (!visited[i]) {\r\n dfs(i, adj, visited, topoStack);\r\n }\r\n }\r\n\r\n // Create a vector to store the result in topological order\r\n vector<int> topoOrder;\r\n while (!topoStack.empty()) {\r\n topoOrder.push_back(topoStack.top());\r\n // cout<<topoStack.top()<<endl;\r\n topoStack.pop();\r\n }\r\n vector<vector<int>> dp(colors.size(),vector<int>(26,0));\r\n vector<int> visited2(colors.size(),0);\r\n int ans=1;\r\n for(int i= topoOrder.size()-1;i>=0;i--){\r\n \r\n if(visited2[topoOrder[i]]==0){\r\n // cout<<topoOrder[i]<<endl;\r\n dfs2(topoOrder[i], adj, visited2, colors, dp);\r\n \r\n for(int j=0;j<26;j++){\r\n ans=max(ans, dp[topoOrder[i]][j]);\r\n }\r\n }\r\n }\r\n return ans;\r\n\r\n }\r\n};", "memory": "165514" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "// OJ: https://leetcode.com/contest/weekly-contest-240/problems/largest-color-value-in-a-directed-graph/\r\n// Author: github.com/lzl124631x\r\n// Time: O(V + E)\r\n// Space: O(V + E)\r\nclass Solution {\r\n typedef array<int, 26> T;\r\npublic:\r\n int largestPathValue(string C, vector<vector<int>>& E) {\r\n unordered_map<int, vector<int>> G;\r\n vector<int> indegree(C.size());\r\n for (auto &e : E) {\r\n G[e[0]].push_back(e[1]); // build graph\r\n indegree[e[1]]++; // count indegrees\r\n }\r\n vector<T> cnt(C.size(), T{}); // cnt[i][j] is the maximum count of j-th color from the ancester nodes to node i.\r\n queue<int> q;\r\n for (int i = 0; i < C.size(); ++i) {\r\n if (indegree[i] == 0) { // if this node has 0 indegree, we can use it as a source node\r\n q.push(i);\r\n cnt[i][C[i] - 'a'] = 1; // the count of the current color should be 1\r\n }\r\n }\r\n int ans = 0, seen = 0;\r\n while (q.size()) {\r\n auto u = q.front();\r\n q.pop();\r\n int val = *max_element(begin(cnt[u]), end(cnt[u])); // we use the maximum of all the maximum color counts as the color value.\r\n ans = max(ans, val);\r\n ++seen;\r\n for (int v : G[u]) {\r\n for (int i = 0; i < 26; ++i) {\r\n cnt[v][i] = max(cnt[v][i], cnt[u][i] + (i == C[v] - 'a')); // try to use node `u` to update all the color counts of node `v`.\r\n }\r\n if (--indegree[v] == 0) {\r\n q.push(v);\r\n }\r\n }\r\n }\r\n return seen < C.size() ? -1 : ans;\r\n }\r\n};", "memory": "167393" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n unordered_map<char,int> umap;\r\n vector<int> topo_sort(vector<vector<int>>& adj ){\r\n // int count=0;\r\n // for(auto j:adj){\r\n // cout<<count++<<\" -> \";\r\n // for( auto i:j){\r\n // cout<<i<<\" \";\r\n // }\r\n // cout<<endl;\r\n // }\r\n // indegree;\r\n int n= adj.size();\r\n vector<int> indegree( n,0 );\r\n for(auto ad:adj){\r\n for( auto i:ad){\r\n indegree[i]++;\r\n }\r\n }\r\n vector<bool> visited(n,0);\r\n queue<int> qu;\r\n for( int i=0;i<n;i++){\r\n if(indegree[i]==0){\r\n indegree[i]--;\r\n visited[i]=1;\r\n qu.push(i);\r\n }\r\n }\r\n vector<int> ret;\r\n while(!qu.empty()){\r\n int tp = qu.front();\r\n qu.pop();\r\n ret.push_back(tp);\r\n for(auto j: adj[tp]){\r\n indegree[j]--;\r\n if(indegree[j]==0 && visited[j]==0){\r\n visited[j]=1;\r\n qu.push(j);\r\n }\r\n }\r\n }\r\n // if(ret.size() != n) return {};\r\n return ret;\r\n }\r\n int dfs(vector<vector<int>>& graph_item, vector<vector<int>>& dp, int i, int clr, string& colors){\r\n if(dp[i][clr] != -1) return dp[i][clr];\r\n int maxx = 0;\r\n for( auto j:graph_item[i]){\r\n dp[j][clr] = dfs( graph_item, dp, j, clr, colors);\r\n maxx = max(maxx,dp[j][clr]);\r\n }\r\n if( clr == umap[colors[i]]) maxx +=1;\r\n return maxx;\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n vector<vector<int>> graph_item(n);\r\n for(auto edg: edges){\r\n // create item to item adj list\r\n int a = edg[0];\r\n int b = edg[1];\r\n graph_item[a].push_back(b);\r\n }\r\n\r\n vector<int> topo_item = topo_sort(graph_item);\r\n if(topo_item.size()!=n) return -1;\r\n // for(auto j: topo_item)\r\n // cout<<j<<\" \";\r\n\r\n // no of distinct color\r\n // unordered_map<char,int> umap; // declared above\r\n int count=0;\r\n for(char c:colors){\r\n if(umap.find(c)==umap.end()){\r\n umap[c] = count;\r\n count++;\r\n }\r\n }\r\n int m = count;\r\n vector<vector<int>> dp(n,vector<int> (m,-1));\r\n int ret=0;\r\n for(int i=0;i<n;i++){\r\n int clr = umap[colors[i]];\r\n if(dp[i][clr]!=-1) continue;\r\n dp[i][clr] = dfs( graph_item, dp, i, clr, colors);\r\n ret = max(ret,dp[i][clr]);\r\n }\r\n // int ret=0;\r\n // for(auto i:dp){\r\n // for(auto j:i){\r\n // ret = max(ret,j);\r\n // }\r\n // }\r\n return ret;\r\n }\r\n};", "memory": "167393" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\nbool dfs(vector<vector<int>>& adj,int n,int cur,vector<int>&vis,vector<int>&temp)\r\n{\r\n vis[cur]=1;\r\n temp[cur]=1;\r\n for(int i=0;i<adj[cur].size();i++)\r\n {\r\n int node=adj[cur][i];\r\n if(temp[node]==1)return true;\r\n if(vis[node]==1)continue;\r\n else{\r\n if(dfs(adj,n,node,vis,temp))return true;\r\n }\r\n }\r\n temp[cur]=0;\r\n return false;\r\n}\r\nbool cycle(vector<vector<int>>& adj,int n)\r\n{\r\n vector<int> vis(n,0);\r\n for(int i=0;i<n;i++)\r\n {\r\n if(vis[i]==0)\r\n {\r\n vector<int> temp(n,0);\r\n if(dfs(adj,n,i,vis,temp))return true;\r\n }\r\n }\r\n return false;\r\n}\r\n\r\nvoid solve(vector<vector<int>>& adj,int n,string& colors,int node,int &ans,vector<vector<int>>& dp)\r\n{\r\nif(dp[node][26]!=-1)return;\r\nint c=(colors[node]-'a');\r\nfor(int i=0;i<adj[node].size();i++)\r\n{\r\n int x=adj[node][i];\r\n solve(adj,n,colors,x,ans,dp);\r\n for(int j=0;j<27;j++)\r\n {\r\n dp[node][j]=max(dp[node][j],dp[x][j]);\r\n }\r\n}\r\ndp[node][c]++;\r\ndp[node][26]=max(dp[node][26],dp[node][c]);\r\nans=max(ans,dp[node][26]);\r\nreturn;\r\n}\r\n\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n=colors.length();\r\n vector<vector<int>> adj(n);\r\n int m=edges.size();\r\n for(int i=0;i<m;i++)\r\n {\r\n adj[edges[i][0]].push_back(edges[i][1]);\r\n }\r\n if(cycle(adj,n))return -1;\r\n int ans=0;\r\n vector<vector<int>> dp(n,vector<int>(27,0));\r\n for(int i=0;i<n;i++)\r\n {\r\n dp[i][26]=-1;\r\n }\r\n for(int i=0;i<n;i++)\r\n {\r\n solve(adj,n,colors,i,ans,dp);\r\n }\r\n return ans;\r\n }\r\n};", "memory": "169271" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n\r\n bool dfsCheckCycle(int node, vector<int> adj[], vector<int>& visited, vector<int>& recStack) {\r\n visited[node] = 1; // Mark node as visited\r\n recStack[node] = 1; // Mark node as part of the recursion stack\r\n\r\n // Visit all neighbors\r\n for (int neighbor : adj[node]) {\r\n // If neighbor is not visited, recurse on it\r\n if (!visited[neighbor]) {\r\n if (dfsCheckCycle(neighbor, adj, visited, recStack)) {\r\n return true; // Cycle found\r\n }\r\n }\r\n // If neighbor is already in the recursion stack, we found a cycle\r\n else if (recStack[neighbor]) {\r\n return true; // Cycle found\r\n }\r\n }\r\n\r\n recStack[node] = 0; // Remove the node from the recursion stack after processing\r\n return false;\r\n }\r\n\r\n // Function to check if a cycle exists in the graph\r\n bool isCyclic(vector<int> adj[], int n) {\r\n vector<int> visited(n, 0); // Visited array\r\n vector<int> recStack(n, 0); // Recursion stack\r\n\r\n // Perform DFS for every unvisited node\r\n for (int i = 0; i < n; ++i) {\r\n if (!visited[i]) {\r\n if (dfsCheckCycle(i, adj, visited, recStack)) {\r\n return true; // Cycle detected\r\n }\r\n }\r\n }\r\n\r\n return false; // No cycle found\r\n }\r\n\r\n void dfs(int node, vector<int> adj[], vector<int>& visited, stack<int>& topoStack) {\r\n visited[node] = 1; // Mark the node as visited\r\n\r\n // Visit all neighbors\r\n for (int neighbor : adj[node]) {\r\n if (!visited[neighbor]) {\r\n dfs(neighbor, adj, visited, topoStack);\r\n }\r\n }\r\n\r\n // After all neighbors are processed, push the current node to the stack\r\n topoStack.push(node);\r\n }\r\n\r\n void dfs2(int node, vector<int> adj[], vector<int>& visited, string &colors, vector<vector<int>> &dp){\r\n visited[node]=1;\r\n int curr_color = colors[node]-'a';\r\n dp[node][curr_color] = 1;\r\n for(int i=0;i<27;i++){\r\n for(int j=0;j<adj[node].size();j++){\r\n \r\n int neighbour = adj[node][j];\r\n // cout<<node<<\" \"<<neighbour<<endl;\r\n int temp = 0;\r\n if(i==curr_color) temp=1;\r\n if(visited[neighbour]==0) dfs2(neighbour, adj, visited, colors, dp);\r\n dp[node][i] = max(dp[neighbour][i] +temp, dp[node][i]);\r\n }\r\n }\r\n return;\r\n }\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n vector<int> adj[colors.size()];\r\n vector<int> visited(colors.size(),0);\r\n for (const auto& edge : edges) {\r\n int u = edge[0]; // from node\r\n int v = edge[1]; // to node\r\n adj[u].push_back(v);\r\n // adj[v].push_back(u);\r\n }\r\n if(isCyclic(adj, colors.size())) return -1;\r\n stack<int> topoStack; // Stack to store the topological sort\r\n\r\n // Perform DFS on all nodes\r\n for (int i = 0; i < colors.size(); ++i) {\r\n if (!visited[i]) {\r\n dfs(i, adj, visited, topoStack);\r\n }\r\n }\r\n\r\n // Create a vector to store the result in topological order\r\n vector<int> topoOrder;\r\n while (!topoStack.empty()) {\r\n topoOrder.push_back(topoStack.top());\r\n // cout<<topoStack.top()<<endl;\r\n topoStack.pop();\r\n }\r\n vector<vector<int>> dp(colors.size(),vector<int>(27,0));\r\n vector<int> visited2(colors.size(),0);\r\n int ans=1;\r\n for(int i=0;i<topoOrder.size();i++){\r\n \r\n if(visited2[topoOrder[i]]==0){\r\n // cout<<topoOrder[i]<<endl;\r\n dfs2(topoOrder[i], adj, visited2, colors, dp);\r\n \r\n for(int j=0;j<27;j++){\r\n ans=max(ans, dp[topoOrder[i]][j]);\r\n }\r\n }\r\n }\r\n return ans;\r\n\r\n }\r\n};", "memory": "169271" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n // int f(int node,vector<vector<int>> &dp,vector<int> adj[], vector<bool> &vis, string &colors)\r\n // {\r\n\r\n // }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n=colors.size();\r\n int m=edges.size();\r\n vector<int> adj[n];\r\n vector<int> indegree(n,0);\r\n for(int i=0;i<m;i++)\r\n {\r\n indegree[edges[i][1]]++;\r\n adj[edges[i][0]].push_back(edges[i][1]);\r\n }\r\n\r\n queue<int> q;\r\n for(int i=0;i<n;i++)\r\n {\r\n if(indegree[i]==0)\r\n {\r\n // cout<<i<<endl;\r\n q.push(i);\r\n }\r\n }\r\n \r\n vector<vector<int>> dp(n,vector<int>(26,0));\r\n int counter=0;\r\n int ans=-1;\r\n while(q.size()!=0)\r\n {\r\n int node=q.front();\r\n q.pop();\r\n counter++;\r\n\r\n dp[node][colors[node]-'a']++;\r\n ans=max(dp[node][colors[node]-'a'],ans);\r\n for(auto it:adj[node])\r\n {\r\n for(int i=0;i<26;i++)\r\n {\r\n dp[it][i]=max(dp[it][i],dp[node][i]);\r\n }\r\n indegree[it]--;\r\n if(indegree[it]==0)\r\n q.push(it);\r\n }\r\n\r\n }\r\n if(counter!=n)\r\n return -1;\r\n\r\n\r\n return ans;\r\n\r\n\r\n }\r\n};", "memory": "171150" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "// Use reverse topo sort to pass on the hashmap\n// If already visited, record the maximum values\n// At the end, check for cycle\n\nclass Solution {\npublic:\n int largestPathValue(string colors, vector<vector<int>>& edges) {\n int res = 0, n = colors.size();\n vector<vector<int>> dp(n, vector<int>(26, 0));\n vector<int> adj[n], indegree(n, 0);\n for (int i = 0; i < edges.size(); i++) {\n int u = edges[i][0], v = edges[i][1];\n adj[v].push_back(u);\n indegree[u]++;\n }\n queue<int> q;\n for (int u = 0; u < n; u++) {\n if(indegree[u] == 0) {\n q.push(u);\n }\n }\n\n int cnt = 0;\n while(q.size()) {\n int u = q.front();\n q.pop();\n res = max(res, ++dp[u][colors[u] - 'a']);\n for(auto& v : adj[u]) {\n if(--indegree[v] == 0) {\n for(int i = 0; i < 26; i++) {\n dp[v][i] = max(dp[v][i], dp[u][i]);\n }\n q.push(v);\n }\n else {\n for(int i = 0; i < 26; i++) {\n dp[v][i] = max(dp[v][i], dp[u][i]);\n }\n }\n }\n cnt++;\n }\n if(cnt != n) {\n return -1;\n }\n return res;\n }\n};", "memory": "171150" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n bool hasCycle(int src, vector<int> adj[], vector<int>& mark) {\r\n mark[src] = 1;\r\n for(auto child: adj[src]) {\r\n if(mark[child] == 2) {continue;}\r\n if(mark[child] == 1 || hasCycle(child, adj, mark)) {return true;}\r\n }\r\n mark[src] = 2;\r\n return false;\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = (int)colors.length();\r\n vector<int> adj[n];\r\n vector<int> indegree(n, 0);\r\n for(auto edge: edges) {\r\n adj[edge[0]].push_back(edge[1]);\r\n indegree[edge[1]]++;\r\n }\r\n\r\n vector<int> mark(n, 0);\r\n for(int i=0;i<n;i++) {\r\n if(!mark[i] && hasCycle(i, adj, mark)) {return -1;}\r\n }\r\n mark.clear();\r\n\r\n queue<int> q;\r\n vector<vector<int> > dp(n, vector<int>(26, 0));\r\n for(int i=0;i<n;i++) {\r\n if(indegree[i] == 0) {q.push(i); dp[i][colors[i]-'a']=1;}\r\n }\r\n\r\n int ans = 0;\r\n while(!q.empty()) {\r\n int src = q.front();q.pop();\r\n for(auto child: adj[src]) {\r\n indegree[child]--;\r\n for(int i=0;i<26;i++) {\r\n dp[child][i] = max(dp[child][i], dp[src][i] + (colors[child]-'a' == i ? 1 : 0));\r\n }\r\n if(indegree[child] == 0) {\r\n q.push(child);\r\n }\r\n }\r\n for(int i=0;i<26;i++) {\r\n ans = max(ans, dp[src][i]);\r\n }\r\n }\r\n return ans;\r\n }\r\n};", "memory": "173029" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n bool dfs(int node, vector<int>&vis, vector<int>&path_vis, vector<int>adj[]){\r\n vis[node] = 1;\r\n path_vis[node] = 1;\r\n bool flag = false;\r\n\r\n for(auto it : adj[node]){\r\n if(!vis[it]){\r\n flag |= dfs(it, vis, path_vis, adj);\r\n }\r\n else if(path_vis[it])return true;\r\n\r\n }\r\n path_vis[node] = 0;\r\n return flag;\r\n }\r\n\r\n void fun(int node, string &col, vector<int>&vis, vector<vector<int>>&info, int &ans, vector<int>adj[]){\r\n vis[node] = 1;\r\n\r\n for(auto it : adj[node]){\r\n if(!vis[it]){\r\n fun(it, col, vis, info, ans, adj);\r\n }\r\n\r\n for(int i = 0; i < 26; i++){\r\n info[node][i] = max(info[node][i], info[it][i]);\r\n }\r\n }\r\n\r\n info[node][col[node] - 'a']++;\r\n\r\n for(int i = 0; i < 26; i++)ans = max(ans, info[node][i]);\r\n\r\n }\r\n\r\n int largestPathValue(string col, vector<vector<int>>& edges) {\r\n int n = col.size();\r\n vector<int>adj[n];\r\n for(auto x : edges){\r\n adj[x[0]].push_back(x[1]);\r\n }\r\n\r\n vector<int>vis(n, 0), path(n, 0);\r\n\r\n for(int i = 0; i < n; i++){\r\n if(!vis[i]){\r\n bool flag = dfs(i, vis, path, adj);\r\n if(flag)return -1;\r\n }\r\n }\r\n\r\n for(auto &x : vis)x = 0;\r\n\r\n vector<vector<int>>info(n, vector<int>(26, 0));\r\n \r\n int ans = 0;\r\n for(int i = 0; i < n; i++){\r\n if(!vis[i]){\r\n fun(i, col, vis, info, ans, adj);\r\n }\r\n }\r\n return ans;\r\n }\r\n};", "memory": "173029" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>m == edges.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>colors</code> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
1
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.length(); // n should be the number of nodes, not edges\r\n\r\n vector<int> indegree(n, 0);\r\n vector<vector<int>> adj(n);\r\n\r\n for(auto &edge : edges){\r\n adj[edge[0]].push_back(edge[1]);\r\n indegree[edge[1]]++;\r\n }\r\n\r\n vector<vector<int>> t(n, vector<int>(26, 0));\r\n\r\n queue<int> q;\r\n\r\n for(int i = 0; i < n; i++){\r\n if(indegree[i] == 0){\r\n q.push(i);\r\n }\r\n }\r\n\r\n int ans = 0;\r\n int count_nodes = 0;\r\n\r\n while(!q.empty()){\r\n int node = q.front();\r\n q.pop();\r\n count_nodes++;\r\n \r\n // Update the answer for the current node\r\n t[node][colors[node] - 'a']++;\r\n ans = max(ans, t[node][colors[node] - 'a']);\r\n\r\n for(auto &v : adj[node]){\r\n for(int i = 0; i < 26; i++){\r\n t[v][i] = max(t[v][i], t[node][i]);\r\n }\r\n\r\n indegree[v]--;\r\n if(indegree[v] == 0){\r\n q.push(v);\r\n }\r\n }\r\n }\r\n\r\n if(count_nodes < n){\r\n return -1; // Graph has a cycle\r\n }\r\n\r\n return ans;\r\n }\r\n};", "memory": "174908" }