question_id
int64 1
3.58k
| name
stringlengths 3
77
| content
stringlengths 516
21.8k
| acRate
float64 9.95
93
| difficulty
stringclasses 3
values | topics
sequencelengths 1
10
| runtimeDistribution
stringlengths 49
2.26k
| memoryDistribution
stringlengths 88
2.23k
| rt_list
listlengths 0
152
| rt_solution_count
int64 0
152
| mm_list
listlengths 0
153
| mm_solution_count
int64 0
153
|
---|---|---|---|---|---|---|---|---|---|---|---|
1,058 | lexicographically-smallest-equivalent-string | <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
<ul>
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
</ul>
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
<ul>
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
</ul>
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
<strong>Output:</strong> "makkek"
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
<strong>Output:</strong> "hdld"
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
<strong>Output:</strong> "aauaaaaada"
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
<li><code>s1.length == s2.length</code></li>
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>
| 76.709554 | Medium | [
"string",
"union-find"
] | {"lang": "cpp", "distribution": [["0", 26.3784], ["1", 0.1081], ["2", 6.5946], ["3", 3.4595], ["4", 14.3784], ["5", 14.0541], ["6", 3.3514], ["7", 2.4865], ["8", 2.9189], ["9", 0.5405], ["10", 0.5405], ["11", 1.1892], ["12", 0.3243], ["13", 0.2162], ["14", 0.973], ["15", 1.6216], ["16", 0.973], ["17", 1.1892], ["18", 1.8378], ["19", 2.3784], ["20", 1.0811], ["21", 0.4324], ["22", 1.4054], ["23", 1.4054], ["24", 0.1081], ["25", 0.2162], ["26", 0.2162], ["27", 0.3243], ["28", 0.973], ["29", 0.1081], ["30", 0.4324], ["31", 0.7568], ["32", 1.4054], ["33", 0.2162], ["34", 0.1081]]} | {"lang": "cpp", "distribution": [["7900", 0.8649], ["8000", 3.027], ["8100", 9.1892], ["8200", 19.7838], ["8300", 17.0811], ["8400", 4.4324], ["8500", 0.4324], ["8600", 0.973], ["8700", 0.3243], ["8800", 0.6486], ["8900", 0.4324], ["9000", 0.3243], ["9100", 0.7568], ["9200", 0.5405], ["9300", 0.8649], ["9400", 2.3784], ["9500", 3.1351], ["9600", 0.973], ["9700", 1.2973], ["9800", 0.2162], ["9900", 0.5405], ["10000", 0.6486], ["10100", 0.3243], ["10200", 0.5405], ["10300", 0.5405], ["10400", 0.8649], ["10500", 0.7568], ["10600", 0.6486], ["10700", 1.0811], ["10800", 0.8649], ["10900", 1.4054], ["11000", 1.2973], ["11100", 0.1081], ["11200", 0.2162], ["11300", 0.6486], ["11400", 0.5405], ["11500", 0.8649], ["11600", 1.4054], ["11700", 0.5405], ["11800", 2.1622], ["11900", 2.9189], ["12000", 5.7297], ["12100", 1.0811], ["12200", 0.3243], ["12300", 0.4324], ["12400", 0.3243], ["12600", 0.1081], ["12700", 0.1081], ["12800", 0.2162]]} | [
{
"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};",
"runtime": "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};",
"runtime": "0"
},
{
"code": "class Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n vector<int> parent;\n for (int i = 0; i < 26; i++) {\n parent.push_back(i);\n }\n\n int n = s1.size();\n for (int i = 0; i < n; i++) {\n unite(parent, s1[i] - 'a', s2[i] - 'a');\n }\n\n int m = baseStr.size();\n for (int i = 0; i < m; i++) {\n baseStr[i] = find(parent, baseStr[i] - 'a') + 'a';\n }\n\n return baseStr;\n }\n\n int find(vector<int>& g, int x) {\n if (g[x] != x) {\n g[x] = find(g, g[x]);\n }\n\n return g[x];\n }\n\n void unite(vector<int>& g, int x, int y) {\n int root1 = find(g, x);\n int root2 = find(g, y);\n\n if (root1 < root2) {\n g[root2] = root1;\n } else {\n g[root1] = root2;\n }\n \n }\n};",
"runtime": "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};",
"runtime": "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};",
"runtime": "3"
},
{
"code": "class Solution {\npublic:\n vector<int>par;\n int find(int x){\n if(par[x]==-1) return x;\n return find(par[x]);\n }\n void Union(int u,int v){\n int x=find(u),y=find(v);\n if(x==y) return ;\n par[max(x,y)]=min(x,y);\n }\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n par.resize(26,-1);\n for(int i=0;i<s1.size();i++){\n Union(s1[i]-'a',s2[i]-'a');\n }\n string ans=\"\";\n for(int i=0;i<baseStr.size();i++){\n ans+='a'+find(baseStr[i]-'a');\n }\n return ans;\n }\n};",
"runtime": "4"
},
{
"code": "class Solution {\npublic:\n\n char find(char x,vector<char>&parent){\n if(parent[x -'a']==x){\n return x;\n }\n return parent[x-'a']=find(parent[x-'a'],parent);\n }\n\n void unite(char a, char b, vector<char>&parent){\n char p=find(a,parent);\n char q=find(b,parent);\n if(p!=q){\n parent[max(p,q)-'a']=min(p,q);\n }\n \n }\n\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n string ans;\n vector<char>parent(26);\n for(char c='a';c<='z';c++){\n parent[c-'a']=c;\n }\n for(int i=0;i<s1.length();i++){\n unite(s1[i],s2[i],parent);\n }\n\n for (char c : baseStr) {\n ans.push_back(find(c, parent));\n }\n return ans;\n }\n};",
"runtime": "4"
},
{
"code": "class UnionFind {\nprivate:\n int root[26] = {0};\npublic:\n UnionFind()\n {\n for(int i = 0; i < 26; i++)\n {\n root[i] = i;\n }\n }\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 char Find(const char& ch)\n {\n return 'a' + Find(ch - 'a');\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 return;\n }\n if(root[r1] < root[r2])\n {\n root[r2] = r1;\n }\n else\n {\n root[r1] = r2;\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 = graph.Find(ch);\n }\n return baseStr;\n }\n};",
"runtime": "5"
},
{
"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};",
"runtime": "6"
},
{
"code": "class Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n unordered_set<int> g[26];\n for (int i = 0; i < s1.size(); ++i) {\n auto c1 = s1[i] - 'a';\n auto c2 = s2[i] - 'a';\n g[c1].insert(c2);\n g[c2].insert(c1);\n }\n int mins[26];\n for (int i = 0; i < 26; ++i) {\n mins[i] = i;\n bool visited[26] = {false};\n queue<int> q;\n q.push(i);\n while (!q.empty()) {\n int cur = q.front();\n q.pop();\n if (visited[cur]) {\n continue;\n }\n visited[cur] = true;\n mins[i] = min(mins[i], cur);\n for (auto peer : g[cur]) {\n if (!visited[peer]) {\n q.push(peer);\n }\n }\n }\n }\n for (char& c : baseStr) {\n c = mins[c - 'a'] + 'a';\n }\n return baseStr;\n }\n};",
"runtime": "7"
},
{
"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};",
"runtime": "8"
},
{
"code": "class Solution {\npublic:\n unordered_map<char, char> parents;\n unordered_map<char, int> ranks;\n \n void addSet() {\n for (int i = 0; i < 26; i++) {\n parents[(char)(i + 'a')] = (char)(i + 'a');\n ranks[(char)(i + 'a')] = 0;\n }\n } \n \n char findSet(char val) {\n \n if (parents[val] == val) {\n return val;\n }\n return parents[val] = findSet(parents[val]);\n }\n \n void unionSet(char val1, char val2) {\n if (val1 == val2) {\n return;\n }\n char parent1 = findSet(val1);\n char parent2 = findSet(val2);\n \n // cout << \"Child \" << val1 << \" Parent \" << parent1 << \", Child \" << val2 << \" Parent \" << parent2 << endl;\n \n if (parent1 == parent2) {\n return;\n }\n \n if (ranks[parent1] > ranks[parent2]) {\n parents[parent2] = parent1;\n } else if (ranks[parent2] > ranks[parent1]) {\n parents[parent1] = parent2;\n } else {\n parents[parent2] = parent1;\n ranks[parent1]++;\n ranks[parent2] = 0;\n }\n }\n\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n string res = \"\";\n addSet();\n \n // cout << \"ADDING DONE\" << endl;\n \n for (int i = 0; i < s1.size(); i++) {\n // cout << s1[i] << \" \" << s2[i] << endl;\n unionSet(s1[i], s2[i]);\n // cout << \"DONE\" << endl;\n }\n \n // cout << \"UNIONING DONE\" << endl;\n \n // vector<vector<res>> v(26, {});\n unordered_map<char, vector<char>> charMappings;\n \n for (int i = 0; i < 26; i++) {\n char child = (char)(i + 'a');\n char parent = findSet(child);\n \n if (charMappings.count(parent) == 0) {\n charMappings[parent] = {};\n }\n \n charMappings[parent].push_back(child);\n }\n \n \n \n for (int i = 0; i < 26; i++) {\n char key = (char)(i + 'a');\n if (charMappings.count(key)) {\n sort(charMappings[key].begin(), charMappings[key].end());\n }\n }\n \n for (int i = 0; i < baseStr.size(); i++) {\n char parent = findSet(baseStr[i]);\n \n res += charMappings[parent][0];\n }\n \n return res;\n }\n};",
"runtime": "9"
},
{
"code": "class Solution {\npublic:\n char dfs(unordered_map<char, vector<char>>& mapping, char input, unordered_set<char>& visited) {\n if (visited.count(input) > 0) {\n return input;\n }\n visited.insert(input);\n char ret = input;\n for (const auto ch : mapping[input]) {\n ret = min(ret, dfs(mapping, ch, visited));\n }\n return ret;\n }\npublic:\n string smallestEquivalentString(string A, string B, string S) {\n unordered_map<char, vector<char>> mapping;\n for (int i = 0; i < A.size(); ++i) {\n mapping[A[i]].push_back(B[i]);\n mapping[B[i]].push_back(A[i]);\n }\n string ret;\n unordered_map<char, char> memo;\n for (const auto s : S) {\n if (memo.count(s) > 0) {\n ret += memo[s];\n continue;\n }\n unordered_set<char> visited;\n const auto min_char = dfs(mapping, s, visited);\n for (const auto ch : visited) {\n memo[ch] = min_char;\n }\n ret += min_char;\n }\n return ret;\n }\n};",
"runtime": "10"
},
{
"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",
"runtime": "11"
},
{
"code": "class Solution {\npublic:\n\n void dfs(int node,vector<int> &vis,vector<int> adj[],int &var)\n {\n vis[node]=1;\n \n var=min(node,var);\n for(auto it:adj[node])\n {\n if(!vis[it])\n {\n dfs(it,vis,adj,var);\n }\n \n }\n }\n\n\n\n\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n // let's try to solve this by DFS\n int n=s1.size();\n vector<int>adj[27];\n for(int i=0;i<s1.size();i++)\n {\n adj[s1[i]-'a'].push_back(s2[i]-'a');\n adj[s2[i]-'a'].push_back(s1[i]-'a');\n }\n\n string ans=\"\";\n \n for(int i=0;i<baseStr.size();i++)\n {\n int var=baseStr[i]-'a';\n vector<int> vis(27,0);\n dfs(baseStr[i]-'a',vis,adj,var);\n ans+= char(var+'a');\n }\n return ans;\n }\n};",
"runtime": "12"
},
{
"code": "class Solution {\nprivate:\n char solve(char ch,map<char,vector<char>>& mpp,unordered_set<char>& visited)\n {\n char temp=ch;\n visited.insert(ch);\n for(auto it:mpp[ch])\n {\n if(visited.find(it)==visited.end())\n {\n temp=min(temp,solve(it,mpp,visited));\n }\n }\n return temp;\n }\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n map<char,vector<char>> mpp;\n for(int i=0;i<s1.size();i++)\n {\n mpp[s1[i]].push_back(s2[i]);\n mpp[s2[i]].push_back(s1[i]);\n }\n vector<char> arr;\n for(char ch='a';ch<='z';ch++)\n {\n unordered_set<char> visited;\n char ans=solve(ch,mpp,visited);\n arr.push_back(ans);\n }\n string res=\"\";\n for(int i=0;i<baseStr.size();i++)\n {\n res+=arr[baseStr[i]-'a'];\n }\n return res;\n }\n};",
"runtime": "13"
},
{
"code": "class Solution {\npublic:\n char dfs(unordered_map<char, vector<char>>&adj, char &currch, vector<int>&vis)\n {\n vis[currch - 'a'] = 1;\n char minimumchar = currch;\n for(auto & i: adj[currch])\n {\n if(vis[i-'a']==0)\n {\n minimumchar = min( minimumchar, dfs(adj, i , vis));\n }\n }\n return minimumchar;\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 {\n char u= s1[i];\n char v= s2[i];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n string ans;\n for(int i=0;i < m;i++)\n {\n vector<int>vis(26,0);\n char currch = baseStr[i];\n char mini = dfs(adj, currch, vis);\n ans.push_back(mini);\n }\n return ans;\n }\n};",
"runtime": "14"
},
{
"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};",
"runtime": "15"
},
{
"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",
"runtime": "16"
},
{
"code": "class Solution {\npublic:\n char dfs(unordered_map<char,vector<char>> &adj , vector<int>&vis ,char curr){\n vis[curr - 'a'] = 1;\n char minchar = curr;\n \n for(auto it:adj[curr]){\n\n if(vis[it - 'a'] == 0){\n minchar = min(minchar , dfs(adj ,vis , it));\n }\n }\n\n return minchar;\n }\n string smallestEquivalentString(string s1, string s2, string s) {\n \n unordered_map<char,vector<char>> adj;\n\n int n= s1.size();\n int m =s.size();\n\n for(int i=0;i<n;i++){\n adj[s1[i]].push_back(s2[i]);\n adj[s2[i]].push_back(s1[i]);\n }\n\n string res=\"\";\n \n for(int i=0;i<m;i++){\n \n vector<int>vis(26 , 0);\n char ch = s[i];\n ch = min(ch , dfs(adj,vis,ch));\n\n res.push_back(ch);\n }\n\n return res;\n }\n};",
"runtime": "17"
},
{
"code": "class Solution {\npublic:\n void dfs(int node,int &temp,vector<vector<int>> &adj,vector<int> &vis){\n vis[node]=1;\n temp=min(temp,node);\n for(auto it:adj[node]){\n if(vis[it]==0){\n dfs(it,temp,adj,vis);\n }\n }\n }\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n=s1.size();\n\n vector<vector<int>> adj(26,vector<int>());\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\n string ans=\"\";\n for(int i=0;i<baseStr.size();i++){\n int temp=baseStr[i]-'a';\n vector<int> vis(26,0);\n dfs(baseStr[i]-'a',temp,adj,vis);\n ans+=(temp+'a');\n }\n return ans;\n }\n};",
"runtime": "17"
},
{
"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",
"runtime": "18"
},
{
"code": "class Solution {\npublic:\n void dfs(char &mini,int ch,vector<int> adj[],vector<int>& vis){\n vis[ch]=1;\n mini=min(mini,char(ch+'a'));\n for(auto it:adj[ch]){\n if(vis[it]==-1){\n dfs(mini,it,adj,vis);\n }\n }\n\n }\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n=s1.length();\n vector<int>adj[26];\n for(int i=0;i<n;i++){\n int c1=s1[i]-'a';\n int c2=s2[i]-'a';\n adj[c1].push_back(c2);\n adj[c2].push_back(c1);\n }\n string res=\"\";\n for(int i=0;i<baseStr.length();i++){\n char ch=baseStr[i];\n char mini=ch;\n vector<int>vis(26,-1);\n dfs(mini,ch-'a',adj,vis);\n res.push_back(mini);\n }\n return res;\n }\n};",
"runtime": "19"
},
{
"code": "class Solution {\npublic:\n\nchar find( unordered_map<char, set<char>>& mp, char ch)\n{\n \n vector<int> visited(26,0);\n queue<char> q;\n char ans = 'z';\n\n q.push(ch);\n visited[ch-'a'] = 1;\n\n while(!q.empty())\n {\n char ch = q.front();\n if(ch < ans) ans = ch;\n q.pop();\n\n for(auto it: mp[ch])\n {\n if(visited[it-'a'] == 0)\n {\n q.push(it);\n visited[it-'a'] = 1;\n }\n }\n }\n\n return ans;\n}\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n unordered_map<char, set<char>> mp;\n\n int n = s1.length();\n\n for(int i = 0 ; i < n ; i++)\n {\n \n mp[s1[i]].insert(s2[i]);\n mp[s2[i]].insert(s1[i]);\n }\n\n string ans = \"\";\n\n for(int i = 0 ; i < baseStr.length() ; i++)\n {\n\n char ch = find(mp,baseStr[i]);\n ans.push_back(ch);\n }\n\n return ans;\n }\n};",
"runtime": "20"
},
{
"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};",
"runtime": "20"
},
{
"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};",
"runtime": "21"
},
{
"code": "class Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string base_str) {\n int n = s1.length();\n\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 int m = base_str.length();\n\n string result;\n\n for( int i = 0; i < m; i++ ){\n char ch = base_str[i];\n vector< int > visited( 26,0);\n\n result.push_back( DFS( adj, ch , visited ) );\n\n }\n return result;\n\n }\n\n char DFS(unordered_map<char, vector<char>> &adj, char curr, vector<int>& visited) {\n visited[curr - 'a' ] = 1;\n\n char min_char = curr;\n\n for( char & v : adj[curr] ){\n if( visited[ v - 'a'] == 0 ){\n min_char = min( min_char , DFS( adj, v, visited ) );\n }\n }\n\n return min_char;\n }\n};",
"runtime": "22"
},
{
"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};",
"runtime": "23"
},
{
"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};",
"runtime": "24"
},
{
"code": "class Solution {\npublic:\n //function to find the parent of index a\n int findParent(vector<int>arr,int a)\n {\n if(arr[a]<0)\n {\n return a;\n }\n else\n {\n int parent = findParent(arr,arr[a]);\n return parent;\n }\n }\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n vector<int> arr(26,-1); // alphabet array\n int n = s1.length();\n\n for(int i=0;i<n;i++)\n {\n char c = s1[i]>=s2[i]?s2[i]:s1[i]; // smaller char\n char d = s1[i]<=s2[i]?s2[i]:s1[i]; //bigger char\n int e=c-'a';\n int f = d-'a';\n int a = findParent(arr,e); //parent of the smaller char\n int b = findParent(arr,f);\n if(a<b)\n {\n arr[b] = a; // marking the who is parent of the bigger char\n }\n else\n {\n if(a!=b)\n arr[a] = b; \n }\n \n }\n string ans=\"\";\n for(auto i:baseStr)\n {\n int jk = i-'a';\n char jkk = 'a'+findParent(arr,jk);\n ans+=jkk;\n }\n return ans;\n \n }\n};",
"runtime": "25"
},
{
"code": "class Solution {\nprivate:\n int find(vector<int>& root, int el) {\n cout << el << endl;\n if (el != root[el]) {\n root[el] = find(root, root[el]);\n }\n return root[el];\n }\n void unite(vector<int>& root, int x, int y) {\n x = find(root, x);\n y = find(root, y);\n if (x < y) {\n root[y] = x;\n } else {\n root[x] = y;\n }\n }\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n vector<int> root(26);\n int i;\n for (i = 0; i < 26; i++) {\n root[i] = i;\n }\n for (i = 0; i < s1.size(); i++) {\n unite(root, s1[i] - 'a', s2[i] - 'a');\n }\n for (i = 0; i < baseStr.size(); i++) {\n baseStr[i] = find(root, baseStr[i] - 'a') + 'a';\n }\n return baseStr;\n }\n};",
"runtime": "26"
},
{
"code": "class Solution {\npublic:\n char dfs(char node,vector<bool>& vis, unordered_map<char,vector<char>>& adj)\n {\n vis[node-'a']=1;\n char ch=node;\n for(auto& v:adj[node])\n {\n if(!vis[v-'a'])\n {\n ch=min(ch,dfs(v,vis,adj));\n }\n }\n return ch;\n }\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n unordered_map<char,vector<char>>adj(26);\n for(int i=0;i<s1.size();i++)\n {\n adj[s1[i]].push_back(s2[i]);\n adj[s2[i]].push_back(s1[i]);\n }\n string ans;\n for(int i=0;i<baseStr.size();i++)\n {\n vector<bool>vis(26,0);\n ans.push_back(dfs(baseStr[i],vis,adj));\n }\n return ans;\n }\n};",
"runtime": "27"
},
{
"code": "class Solution {\npublic:\n char solve(vector<vector<char>>& adj, char temp, vector<char>& cache) {\n if (cache[temp - 'a'] != 0) {\n return cache[temp - 'a'];\n }\n\n char ans = temp;\n queue<char> q;\n q.push(temp);\n vector<bool> vis(26, false);\n vis[temp - 'a'] = true;\n\n while (!q.empty()) {\n temp = q.front();\n q.pop();\n ans = min(ans, temp);\n\n for (auto it : adj[temp - 'a']) {\n if (!vis[it - 'a']) {\n q.push(it);\n vis[it - 'a'] = true;\n }\n }\n }\n\n cache[temp - 'a'] = ans;\n return ans;\n }\n\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n vector<vector<char>> adj(26);\n for (int i = 0; i < s1.length(); i++) {\n adj[s1[i] - 'a'].push_back(s2[i]);\n adj[s2[i] - 'a'].push_back(s1[i]);\n }\n\n string ans;\n vector<char> cache(26, 0);\n for (int i = 0; i < baseStr.size(); i++) {\n char ch = solve(adj, baseStr[i], cache);\n ans.push_back(ch);\n }\n\n return ans;\n }\n};\n",
"runtime": "28"
},
{
"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};",
"runtime": "29"
},
{
"code": "class Solution {\npublic:\n \n // unordered_map<char, vector<char>> adj;\n void dfs(char u, unordered_map<char, vector<char>>& adj, vector<bool>& vis, char& ch){\n // if(u=='{')return;\n ch=min(u, ch);\n vis[u-'a']=true;\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};",
"runtime": "33"
},
{
"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};",
"runtime": "33"
},
{
"code": "class Solution {\npublic:\n void dfs(unordered_map<int, vector<int>>& adj, int source, vector<bool>& visited, int& minV) {\n visited[source] = true;\n minV = min(minV, source);\n \n for (int& v : adj[source]) {\n if (!visited[v]) {\n dfs(adj, v, visited, minV);\n }\n }\n } \n\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n unordered_map<int, vector<int>> adj;\n \n for (int i = 0; i < s1.length(); i++) {\n if (s1[i] != s2[i]) {\n adj[s1[i] - 'a'].push_back(s2[i] - 'a');\n adj[s2[i] - 'a'].push_back(s1[i] - 'a');\n }\n }\n \n string result;\n \n for (char c : baseStr) {\n vector<bool> visited(26, false);\n int minV = c - 'a';\n dfs(adj, c - 'a', visited, minV);\n result += (char)(minV + 'a');\n }\n\n return result;\n }\n};",
"runtime": "34"
}
] | 37 | [
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
}
] | 55 |
1,984 | maximum-distance-between-a-pair-of-values | <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 <= i < nums1.length</code> and <code>0 <= j < nums2.length</code>, is <strong>valid</strong> if both <code>i <= j</code> and <code>nums1[i] <= 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] >= arr[i]</code> for every <code>1 <= i < arr.length</code>.</p>
<p> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums1[i], nums2[j] <= 10<sup>5</sup></code></li>
<li>Both <code>nums1</code> and <code>nums2</code> are <strong>non-increasing</strong>.</li>
</ul>
| 53.520252 | Medium | [
"array",
"two-pointers",
"binary-search"
] | {"lang": "cpp", "distribution": [["48", 0.2193], ["52", 0.2193], ["55", 0.2193], ["60", 0.2193], ["61", 0.4386], ["62", 0.4386], ["63", 0.4386], ["67", 0.2193], ["71", 0.2193], ["72", 0.4386], ["73", 0.2193], ["87", 0.2193], ["90", 0.2193], ["99", 0.4386], ["101", 0.4386], ["102", 0.4386], ["103", 0.6579], ["104", 0.4386], ["105", 0.2193], ["106", 0.2193], ["107", 0.8772], ["108", 2.193], ["109", 1.3158], ["110", 1.7544], ["111", 2.4123], ["112", 3.7281], ["113", 2.193], ["114", 2.193], ["115", 2.6316], ["116", 2.8509], ["117", 3.5088], ["118", 2.6316], ["119", 1.9737], ["120", 4.1667], ["121", 4.1667], ["122", 4.386], ["123", 2.4123], ["124", 2.6316], ["125", 3.2895], ["126", 2.6316], ["127", 2.4123], ["128", 3.5088], ["129", 3.2895], ["130", 2.4123], ["131", 1.9737], ["132", 1.7544], ["133", 1.7544], ["134", 0.8772], ["135", 1.5351], ["136", 1.0965], ["137", 0.2193], ["138", 1.5351], ["139", 1.3158], ["140", 1.7544], ["141", 0.8772], ["142", 1.5351], ["143", 0.8772], ["144", 1.5351], ["145", 1.3158], ["146", 0.6579], ["147", 1.3158], ["148", 0.2193], ["149", 0.2193]]} | {"lang": "cpp", "distribution": [["100800", 0.6579], ["100900", 5.4825], ["101000", 15.1316], ["101100", 41.886]]} | [
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int ans = 0;\n int i = 0;\n int j = 0;\n while (i < nums1.size() && j < nums2.size()) {\n if (nums2[j] < nums1[i]) {\n i++;\n }\n else {\n ans = max(ans, j - i);\n j++;\n }\n }\n return ans;\n }\n};",
"runtime": "48"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& arr1, vector<int>& arr2) {\n int m = arr1.size(),n = arr2.size();\n int ans = 0,i = 0,j = 0;\n\n while(i < m && j < n)\n {\n if(arr1[i] > arr2[j]) i++;\n else\n {\n ans = max(ans,j - i);\n j++;\n }\n }\n\n return ans;\n }\n};\n#pragma GCC optimize(\"Ofast\")\nstatic auto _ = [](){\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return nullptr;\n}();",
"runtime": "52"
},
{
"code": "// Idea 1: Greedy: two pointers, O(n)\n// Idea 2: binary search for b[j] in a[0...j]\nclass Solution {\npublic:\n // two pointers, O(n)\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n const auto &a = nums1, b = nums2;\n int res = 0;\n int i = 0;\n int j = 0;\n while (i < a.size() && j < b.size()) {\n if (a[i] <= b[j]) {\n res = max(res, j - i);\n ++j; // so to greedily increase (j-i)\n } else {\n // so to decrease a[i], and make this more likely: a[i] <= b[j]\n ++i;\n }\n }\n return res;\n }\n\n // binary search, O(n log n)\n int maxDistance_BinarySearch(vector<int>& nums1, vector<int>& nums2) {\n const auto &a = nums1, b = nums2;\n int res = 0;\n for (int j = (int)b.size() - 1; j >= 0; --j) {\n auto endRange = a.begin() + min(j, (int)a.size());\n auto iter = lower_bound(a.begin(), endRange, b[j], greater<>{});\n if (iter != endRange) {\n int i = iter - a.begin();\n res = max(res, j - i);\n }\n }\n return res;\n }\n};\n\n// speed up the LeetCode tests for free\nstatic int _g_init_io_optimization = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n return 1;\n}();\n",
"runtime": "55"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n int dist = 0;\n int i =0 ;\n int j=0;\n while(i < nums1.size() && j < nums2.size()){\n if(nums1[i] > nums2[j]) i++;\n else{\n dist = max(dist, j-i);\n j++;\n }\n\n }\n return dist;\n }\n};",
"runtime": "60"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int ans = 0;\n int i = 0;\n int j = 0;\n while (i < nums1.size() && j < nums2.size()) {\n if (nums2[j] < nums1[i]) {\n i++;\n }\n else {\n ans = max(ans, j - i);\n j++;\n }\n }\n return ans;\n }\n};",
"runtime": "61"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& arr1, vector<int>& arr2) {\n int m = arr1.size(),n = arr2.size();\n int ans = 0;\n\n for(int i=m-1;i>=0;i--)\n {\n int s = i,e = n - 1;\n\n while(s <= e)\n {\n int m = s + (e - s) / 2;\n\n if(arr2[m] >= arr1[i])\n {\n s = m + 1;\n }\n else\n {\n e = m - 1;\n }\n }\n\n ans = max(ans,s - i - 1);\n }\n\n return ans;\n }\n};\n#pragma GCC optimize(\"Ofast\")\nstatic auto _ = [](){\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return nullptr;\n}();",
"runtime": "62"
},
{
"code": "// Idea: binary search for b[j] in a[0...j]\nclass Solution {\npublic:\n // two pointers, O(n)\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n const auto &a = nums1, b = nums2;\n int res = 0;\n int i = 0;\n int j = 0;\n while (i < a.size() && j < b.size()) {\n if (a[i] <= b[j]) {\n res = max(res, j - i);\n ++j; // so to greedily increase (j-i)\n } else {\n ++i; // so to decrease a[i]\n }\n }\n return res;\n }\n\n // binary search, O(n log n)\n int maxDistance_BinarySearch(vector<int>& nums1, vector<int>& nums2) {\n const auto &a = nums1, b = nums2;\n int res = 0;\n for (int j = (int)b.size() - 1; j >= 0; --j) {\n auto endRange = a.begin() + min(j, (int)a.size());\n auto iter = lower_bound(a.begin(), endRange, b[j], greater<>{});\n if (iter != endRange) {\n int i = iter - a.begin();\n res = max(res, j - i);\n }\n }\n return res;\n }\n};\n\n// speed up the LeetCode tests for free\nstatic int _g_init_io_optimization = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n return 1;\n}();\n",
"runtime": "63"
},
{
"code": "static const int _ = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int i = 0, j = 0, res = 0;\n while (i < nums1.size() && j < nums2.size())\n if (nums1[i] > nums2[j])\n ++i;\n else\n res = max(res, j++ - i);\n return res;\n }\n};",
"runtime": "67"
},
{
"code": "class Solution {\n int binarySearch(vector<int>& a, int b, int e, int t)\n {\n while(b <= e)\n {\n int m = (b + e) / 2;\n if (a[m] < t) e = m - 1;\n else b = m + 1;\n }\n return b;\n }\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n vector<int> d(nums1.size());\n for (int i=0; i<nums1.size(); ++i)\n {\n int j = binarySearch(nums2, i, nums2.size() - 1, nums1[i]);\n d[i] = j - i - 1;\n }\n\n // for (auto el : d) cout << el << \" \"; cout << endl;\n int res = *max_element(d.begin(), d.end());\n return res == -1 ? 0 : res;\n }\n};",
"runtime": "71"
},
{
"code": "// Idea 1: two pointers, O(n)\n// Idea 2: binary search for b[j] in a[0...j]\nclass Solution {\npublic:\n // two pointers, O(n)\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n const auto &a = nums1, b = nums2;\n int res = 0;\n int i = 0;\n int j = 0;\n while (i < a.size() && j < b.size()) {\n if (a[i] <= b[j]) {\n res = max(res, j - i);\n ++j; // so to greedily increase (j-i)\n } else {\n ++i; // so to decrease a[i]\n }\n }\n return res;\n }\n\n // binary search, O(n log n)\n int maxDistance_BinarySearch(vector<int>& nums1, vector<int>& nums2) {\n const auto &a = nums1, b = nums2;\n int res = 0;\n for (int j = (int)b.size() - 1; j >= 0; --j) {\n auto endRange = a.begin() + min(j, (int)a.size());\n auto iter = lower_bound(a.begin(), endRange, b[j], greater<>{});\n if (iter != endRange) {\n int i = iter - a.begin();\n res = max(res, j - i);\n }\n }\n return res;\n }\n};\n\n// speed up the LeetCode tests for free\nstatic int _g_init_io_optimization = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n return 1;\n}();\n",
"runtime": "72"
},
{
"code": "// Idea 1: two pointers, O(n)\n// Idea 2: binary search for b[j] in a[0...j]\nclass Solution {\npublic:\n // two pointers, O(n)\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n const auto &a = nums1, b = nums2;\n int res = 0;\n int i = 0;\n int j = 0;\n while (i < a.size() && j < b.size()) {\n if (a[i] <= b[j]) {\n res = max(res, j - i);\n ++j; // so to greedily increase (j-i)\n } else {\n ++i; // so to decrease a[i]\n }\n }\n return res;\n }\n\n // binary search, O(n log n)\n int maxDistance_BinarySearch(vector<int>& nums1, vector<int>& nums2) {\n const auto &a = nums1, b = nums2;\n int res = 0;\n for (int j = (int)b.size() - 1; j >= 0; --j) {\n auto endRange = a.begin() + min(j, (int)a.size());\n auto iter = lower_bound(a.begin(), endRange, b[j], greater<>{});\n if (iter != endRange) {\n int i = iter - a.begin();\n res = max(res, j - i);\n }\n }\n return res;\n }\n};\n\n// speed up the LeetCode tests for free\nstatic int _g_init_io_optimization = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n return 1;\n}();\n",
"runtime": "73"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int ans = 0;\n for (int i = 0; i < nums1.size(); i++) {\n int l = 0;\n int r = nums2.size() - 1;\n int cur = -1;\n while (l <= r) {\n int mid = (l + r) / 2;\n if (nums2[mid] >= nums1[i]) {\n cur = max(cur, mid);\n l = mid + 1;\n }\n else {\n r = mid - 1;\n }\n }\n ans = max(ans, cur - i);\n } \n return ans;\n }\n};",
"runtime": "87"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int i=0,j=0;\n int n=nums1.size();\n int m = nums2.size();\n int maxi=0;\n while(i<n && j<m){\n if(nums1[i]<=nums2[j]){\n maxi=max(maxi,j-i); \n j++;\n }\n else{\n i++;\n }\n }\n return maxi;\n }\n};",
"runtime": "90"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int n1=nums1.size(),n2=nums2.size(),max=0,j=n2-1;\n for (int i=n1-1;i>=0;i--){\n while(j!=-1 && i<=j-1 && nums1[i]>nums2[j]) j--;\n //cout<<\"I: \"<<i<<\" Broj na i: \"<<n2-j-1<<'\\n';\n if (max<j-i) max=j-i;\n }\n return max;\n }\n};",
"runtime": "99"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int n = nums1.size();\n int m = nums2.size();\n int i = 0, j = 0; \n int ans = 0;\n\n while (i < n && j < m){\n if (nums1[i] <= nums2[j]) {\n ans = max(ans, j - i);\n j++;\n } else {\n i++;\n }\n }\n\n return ans;\n }\n};",
"runtime": "99"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int n = nums2.size();\n int m = nums1.size();\n int i=0, j =0,ans = 0;\n while(i<m && j < n){\n if(nums1[i]>nums2[j]){\n i++;\n }\n else{\n ans = max(ans,j-i);\n j++;\n }\n }\n return ans;\n\n }\n};",
"runtime": "101"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int maxdiff =0;\n int i=0;\n int j=0;\n while(i<nums1.size() && j<nums2.size()){\n if(nums1[i]<=nums2[j]){\n int diff=j-i;\n maxdiff = max(maxdiff,diff);\n j++;\n }\n else if(nums1[i]>nums2[j]){\n i++;\n }\n \n }\n return maxdiff;\n }\n};",
"runtime": "102"
},
{
"code": "class Solution {\npublic:\n\tint maxDistance(vector<int>& nums1, vector<int>& nums2) {\n\t\tint ans = 0;\n\t\tint i = 0;\n\t\tint j = 0;\n\t\twhile(i < nums1.size() and j < nums2.size()){\n\t\t\tif(nums1[i] <= nums2[j]){\n\t\t\t\tif(i <= j){\n\t\t\t\t\tans = max(ans, j - i);\n\t\t\t\t}\n\t\t\t\tj++;\n\t\t\t}\n\t\t\telse{\n\t\t\t\ti++;\n\t\t\t}\n\t\t}\n\t\treturn ans;\n\t}\n};",
"runtime": "103"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) \n {\n int x=0, y=0, ans=0;\n\n while(x < nums1.size() and y < nums2.size())\n {\n if(nums1[x] <= nums2[y])\n {\n if(x <= y) ans = max(ans, y-x);\n ++y;\n }\n else ++x;\n }\n\n return ans;\n }\n};",
"runtime": "104"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int i = 0, j = 0;\n int maxi = 0;\n\n while (i < nums1.size() && j < nums2.size()) {\n if (nums1[i] <= nums2[j]) {\n maxi = max(maxi, j - i);\n j++; \n } else {\n i++; \n }\n }\n return maxi;\n }\n};\n",
"runtime": "110"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int ans = 0;\n int i = 0, j = 1;\n while (i < nums1.size() && j < nums2.size()){\n if (nums1[i] <= nums2[j]) {\n ans = max(ans, j - i);\n j++;\n }\n else if (i < j)\n {\n i++;\n }\n else\n {\n j++;\n }\n }\n\n return ans;\n }\n};",
"runtime": "111"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int n1=nums1.size(),n2=nums2.size();\n int i=n1-1,j=n2-1,ans=0;\n while(i>=0 && j>=0){\n if(nums2[j]<nums1[i]) j--;\n else{\n ans=max(ans,j-i);\n i--;\n }\n }\n return ans;\n }\n};",
"runtime": "112"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int maxDist = 0;\n int j = 0;\n for (int i = 0; i < nums1.size(); ++i) {\n while (j < nums2.size() && nums2[j] >= nums1[i]) {\n ++j;\n }\n if (j > i) {\n maxDist = max(maxDist, j - i - 1);\n }\n }\n return maxDist;\n }\n};",
"runtime": "113"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int n=nums1.size();\n int m=nums2.size();\n int i=0,j=0,ans=0;\n while(i<n && j<m){\n if(nums2[j]>=nums1[i]){\n ans=max(ans,j-i);\n j++;\n }\n else{\n i++;\n }\n }\n return ans;\n }\n};",
"runtime": "114"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int i=0,j=0,res=0;\n while(i<nums1.size() && j<nums2.size()){\n if(nums1[i]>nums2[j]){\n i++;\n }\n else{\n res=max(res,j++-i);\n }\n }\n return res;\n }\n};",
"runtime": "115"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int n = nums1.size();\n int m = nums2.size();\n \n int i = 0, j = 0, ans = 0;\n while(i < n && j < m) {\n if(nums1[i] <= nums2[j]) {\n ans = max(ans, j - i);\n ++j;\n }\n else ++i, ++j;\n }\n \n return ans;\n }\n};",
"runtime": "116"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n long max_dist = 0;\n long j = 0;\n for (int i = 0; i < nums1.size() && j < nums2.size(); ++i) {\n while (j < nums2.size() && nums1[i] <= nums2[j]) {\n ++j;\n }\n max_dist = max(max_dist, j - i - 1);\n\n if (j <= i) ++j;\n }\n\n return max_dist;\n }\n};",
"runtime": "117"
},
{
"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};",
"runtime": "118"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int n=nums1.size();\n int m=nums2.size();\n int i=0;\n int j=0;\n int 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};",
"runtime": "119"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int ans = 0, p1 = 0, p2 = 0;\n while (p1 < nums1.size() && p2 < nums2.size()) {\n if (nums1[p1] > nums2[p2]) {\n ++p1;\n }\n else {\n ans = max(ans, p2 - p1);\n ++p2;\n }\n }\n return ans;\n }\n};",
"runtime": "120"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int i=0,j=0,res=0;\n while(i<nums1.size() && j<nums2.size()){\n if(nums1[i]<=nums2[j]){\n res=max(res,j-i);\n j++;\n }else{\n i++;\n }\n }\n return res;\n }\n};",
"runtime": "121"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n\n int n = nums1.size();\n int p = nums2.size();\n int maxDist = 0;\n int j = 0; \n \n for (int i = 0; i < n; ++i) {\n \n while (j < p && nums1[i] <= nums2[j]) {\n ++j;\n }\n \n if (j > 0) { \n maxDist = std::max(maxDist, j - i - 1);\n }\n }\n \n return maxDist;\n\n\n \n }\n};",
"runtime": "122"
},
{
"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};",
"runtime": "123"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int p1 = 0, p2 = 0;\n int ans = 0;\n while (p1 < nums1.size() && p2 < nums2.size()) {\n if (nums1[p1] > nums2[p2]) {\n p1++;\n }\n else {\n ans = max(ans, p2-p1);\n p2++;\n }\n }\n return ans;\n }\n};",
"runtime": "124"
},
{
"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};",
"runtime": "124"
},
{
"code": "#define ll long long \nclass Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int n = nums1.size();\n int m = nums2.size();\n int i=0;\n int j =0; \n int ans = 0; \n while(i < n && j < m){\n if(nums1[i] > nums2[j])\n i++;\n else{ \n ans = max(ans ,j - i);\n j++;\n }\n }\n return ans;\n }\n};",
"runtime": "125"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int mx = 0;\n int j=nums2.size()-1, i=nums1.size()-1;\n\n while(j>=0 && i>=0){\n if(nums2[j] >= nums1[i]){\n mx = max(mx, j-i);\n i--;\n }\n else{\n j--;\n }\n // mx = max(mx, j-i);\n }\n\n return mx;\n }\n};",
"runtime": "126"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int i = 0;\n int j = 0;\n int ans = 0;\n while(i<nums1.size() && j<nums2.size()){\n if(nums1[i] <= nums2[j]){\n ans = max(ans, j-i);\n j++;\n }\n else{\n i++;\n }\n\n }\n\n return ans;\n }\n};",
"runtime": "127"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int ans = 0;\n for (int i = 0; i < nums1.size(); i++) {\n int l = i, r = nums2.size() - 1;\n while (l + 1 < r) {\n int j = (l + r) / 2;\n if (nums2[j] >= nums1[i]) {\n l = j;\n } else {\n r = j;\n }\n }\n int j = l;\n while (j < nums2.size() - 1 && nums2[j + 1] >= nums1[i]) {\n j++;\n }\n\n ans = std::max(ans, j - i);\n }\n\n return ans;\n }\n};",
"runtime": "128"
},
{
"code": "class Solution {\nprivate:\n int lowerBound(vector<int>& nums1,int n,int cmp){\n \n int si = 0;\n int ei = n-1;\n int result = n-1;\n while(si<=ei){\n int mid = (ei-si)/2 + si;\n if(nums1[mid] <= cmp){\n result = mid;\n ei = mid-1;\n }else{\n si = mid+1;\n }\n }\n return result;\n }\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int n = nums1.size();\n int m = nums2.size();\n int result = 0;\n for(int j = m-1;j>=0;j--){\n int i = lowerBound(nums1,n,nums2[j]);\n if(i<=j && nums1[i]<=nums2[j]){\n \n result = max(result,j-i);\n }\n \n }\n return result;\n }\n};",
"runtime": "129"
},
{
"code": "class Solution {\nprivate:\n int lowerBound(vector<int>& nums1,int n,int cmp){\n \n int si = 0;\n int ei = n-1;\n int result = n-1;\n while(si<=ei){\n int mid = (ei-si)/2 + si;\n if(nums1[mid] <= cmp){\n result = mid;\n ei = mid-1;\n }else{\n si = mid+1;\n }\n }\n return result;\n }\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int n = nums1.size();\n int m = nums2.size();\n int result = 0;\n for(int j = m-1;j>=0;j--){\n int i = lowerBound(nums1,n,nums2[j]);\n if(i<=j && nums1[i]<=nums2[j]){\n \n result = max(result,j-i);\n }\n \n }\n return result;\n }\n};",
"runtime": "129"
},
{
"code": "#define ll long long \nclass Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int i=0;\n int j =0; \n int ans = 0; \n while(i < nums1.size() && j < nums2.size()){\n if(nums1[i] > nums2[j]){\n i++;\n }\n else{ \n ans = max(ans ,j - i);\n j++;\n }\n }\n return ans;\n }\n};",
"runtime": "130"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) \n{\n int n1 = nums1.size();\n int n2 = nums2.size();\n int ans = 0;\n\n for (int i = 0; i < n1; ++i) \n {\n int left = i;\n int right = n2 - 1;\n int best = i;\n \n // Binary search in nums2 to find the farthest valid index\n while (left <= right) \n {\n int mid = left + (right - left) / 2;\n if (nums2[mid] >= nums1[i]) \n {\n best = mid;\n left = mid + 1;\n } \n else \n {\n right = mid - 1;\n }\n }\n \n ans = max(ans, best - i);\n }\n \n return ans;\n}\n};",
"runtime": "131"
},
{
"code": "class Solution {\npublic:\n int binSearch(vector<int>& nums2, int l, int r, int tar) {\n while (l <= r) {\n int mid = l+(r-l)/2;\n // if (nums2[mid] == tar)\n // return mid;\n if (nums2[mid] >= tar)\n l = mid+1;\n else\n // if (nums2[mid] < tar)\n r = mid-1;\n }\n return r;\n }\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n // Brute force\n // Time: O(n.m)\n // Space: O(1)\n // int maxDist = INT_MIN;\n // int n = nums1.size(), m = nums2.size();\n // for (int i=0; i<n; i++) {\n // for (int j=i; j<m; j++) {\n // if (nums1[i] > nums2[j])\n // break;\n // maxDist = max(maxDist, j-i);\n // }\n // }\n \n // Binary Search\n // Time: O(nlogm)\n // Space: O(1)\n int maxDist = INT_MIN;\n int n = nums1.size(), m = nums2.size();\n for (int i=0; i<n; i++) {\n int justGreater = binSearch(nums2, i, m-1, nums1[i]);\n if (i <= justGreater) maxDist = max(maxDist, justGreater-i);\n }\n if (maxDist == INT_MIN)\n return 0;\n return maxDist;\n }\n};",
"runtime": "132"
},
{
"code": "class Solution {\npublic:\n bool valid(vector<int>&nums1,vector<int>&nums2,int dis){\n int i=0;\n while(i<nums1.size()){\n int j=dis+i;\n if(j<0 || j>=nums2.size())break;\n if(i<=j && nums1[i]<=nums2[j])return true;\n i++;\n }\n\n return false;\n }\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int low=0,high=nums2.size();\n int ans=0;\n while(low<=high){\n int mid=(low+high)/2;\n \n if(valid(nums1,nums2,mid)){\n ans=mid;\n low=mid+1;\n }\n else{\n high=mid-1;\n }\n }\n return ans;\n }\n};",
"runtime": "133"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int n = nums1.size(), m = nums2.size();\n int ans = 0;\n reverse(nums2.begin(), nums2.end());\n for(int i = 0; i<n; i++){\n int ind = lower_bound(nums2.begin(), nums2.end(), nums1[i])-nums2.begin();\n int j = m-1-ind;\n if(j>=i) ans = max(ans, j-i);\n }\n return ans;\n }\n};",
"runtime": "134"
},
{
"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};",
"runtime": "135"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int n1 = nums1.size();\n int n2 = nums2.size();\n int ans = 0;\n for(int i = 0 ; i < n1 ; i++){\n int idx = -1 , lo = i , hi = n2 - 1;\n while(lo <= hi){\n int mid = lo + (hi - lo)/2;\n if(nums2[mid] >= nums1[i]){\n idx = mid;\n lo = mid + 1;\n }\n else hi = mid - 1;\n }\n if(idx != -1) ans = max(ans , idx - i);\n }\n return ans;\n }\n};",
"runtime": "136"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int maxDist = 0;\n \n for (int i = 0; i < nums1.size(); ++i) {\n int left = i;\n int right = nums2.size();\n \n while (left < right) {\n int mid = left + (right - left) / 2;\n if (nums2[mid] >= nums1[i]) {\n left = mid + 1;\n } else {\n right = mid;\n }\n }\n \n maxDist = max(maxDist, left - i - 1);\n }\n \n return maxDist;\n }\n};",
"runtime": "137"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int ans=0;\n for(int i=0;i<nums1.size();i++){\n int j=(nums2.size()-1)-(lower_bound(nums2.rbegin(),nums2.rend(),nums1[i])-nums2.rbegin());\n ans=max(ans,j-i);\n }\n return ans;\n }\n};",
"runtime": "144"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int ans = 0;\n int i, j ;\n for(int i = 0 ; i < nums1.size(); i++){\n int num = nums1[i];\n int left = i , right = nums2.size() -1;\n int mid;\n while(left <= right){\n mid = left + (right-left)/2 ;\n if (num <= nums2[mid]){\n ans = max(ans,mid - i) ;\n left = mid + 1;\n }\n else\n right = mid - 1 ;\n }\n }\n \n return ans;\n }\n};\n\n/*\n\n55,30,5,4,2\n100,20,10,10,5\n*/",
"runtime": "145"
},
{
"code": "class Solution {\n int helper(vector<int>&nums,int tar,int i){\n int st=i,en=nums.size()-1;\n int maxi=INT_MIN;\n while(st<=en){\n int mid=st+(en-st)/2;\n if(nums[mid]>=tar){\n maxi=max(maxi,mid-i);\n st=mid+1;\n }\n else{\n en=mid-1;\n }\n }\n return maxi;\n }\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int maxi=INT_MIN;\n for(int i=0;i<nums1.size();i++){\n maxi=max(maxi,helper(nums2,nums1[i],i));\n }\n return maxi==INT_MIN?0:maxi;\n }\n};",
"runtime": "146"
},
{
"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 ret = 0;\n for (auto i = 0; i < n1; ++i) {\n const auto num1 = nums1[i];\n auto left = i;\n auto right = n2 - 1;\n while (left < right) {\n const auto mid = left + (right - left + 1) / 2;\n if (nums2[mid] < num1) {\n right = mid - 1;\n } else {\n left = mid;\n }\n }\n ret = std::max(right - i, ret);\n }\n return ret; \n }\n};",
"runtime": "147"
},
{
"code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int maxi=0;\n for(int i=0;i<nums1.size();i++){\n int l=i,r=nums2.size()-1;\n while(l<=r){\n int mid=l+(r-l)/2;\n if(nums1[i]<=nums2[mid]){\n maxi=max(maxi,mid-i);\n l=mid+1;\n }else{\n r=mid-1;\n }\n }\n }\n return maxi;\n }\n};",
"runtime": "148"
},
{
"code": "class Solution {\npublic:\n\nint ceil(vector<int> &arr2, int m, int x,int i) {\n\n\tint ind = -1,left = i,right = m-1;\n while(left<=right){\n int mid = (left+right)/2;\n if(arr2[mid]>=x){\n ind = mid;\n left = mid+1;\n }\n else{\n right = mid-1;\n }\n }\n return ind;\n}\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int ans = 0;\n for(int i=0;i<nums1.size();i++){\n int a = ceil(nums2,nums2.size(),nums1[i],i);\n if(a>=i){\n ans = max(ans,a-i);\n }\n }\n return ans;\n }\n};",
"runtime": "149"
}
] | 55 | [
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
},
{
"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"
}
] | 6 |
1,986 | largest-color-value-in-a-directed-graph | "<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> ed(...TRUNCATED) | 49.838637 | Hard | [
"hash-table",
"dynamic-programming",
"graph",
"topological-sort",
"memoization",
"counting"
] | "{\"lang\": \"cpp\", \"distribution\": [[\"258\", 0.5108], [\"270\", 0.2554], [\"282\", 0.3831], [\"(...TRUNCATED) | "{\"lang\": \"cpp\", \"distribution\": [[\"127939\", 0.6385000000000001], [\"129818\", 0.2554], [\"1(...TRUNCATED) | [{"code":"class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>(...TRUNCATED) | 100 | [{"code":"class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>(...TRUNCATED) | 112 |
1,993 | sum-of-all-subset-xor-totals | "<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong>(...TRUNCATED) | 87.778276 | Easy | [
"array",
"math",
"backtracking",
"bit-manipulation",
"combinatorics",
"enumeration"
] | "{\"lang\": \"cpp\", \"distribution\": [[\"0\", 30.1166], [\"1\", 19.7487], [\"2\", 13.5099], [\"3\"(...TRUNCATED) | "{\"lang\": \"cpp\", \"distribution\": [[\"8413\", 75.763], [\"8841\", 3.0071000000000003], [\"9268\(...TRUNCATED) | [{"code":"class Solution {\n int solve(vector<int>& nums, int i, int xorr)\n {\n if(i =(...TRUNCATED) | 72 | [{"code":"class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n int n = nums(...TRUNCATED) | 82 |
2,505 | number-of-good-paths | "<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code>(...TRUNCATED) | 56.26695 | Hard | [
"array",
"hash-table",
"tree",
"union-find",
"graph",
"sorting"
] | "{\"lang\": \"cpp\", \"distribution\": [[\"221\", 0.64], [\"228\", 0.48], [\"235\", 0.8], [\"243\", (...TRUNCATED) | "{\"lang\": \"cpp\", \"distribution\": [[\"130665\", 0.48], [\"132597\", 0.16], [\"134530\", 0.48], (...TRUNCATED) | [{"code":"#include <ranges>\n\nclass DisjointSet {\npublic:\n DisjointSet(int n, const vector<int(...TRUNCATED) | 135 | [{"code":"using P2I = pair<int, int>;\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<i(...TRUNCATED) | 120 |
3,276 | minimum-number-of-pushes-to-type-word-ii | "<p>You are given a string <code>word</code> containing lowercase English letters.</p>\n\n<p>Telepho(...TRUNCATED) | 80.708191 | Medium | [
"hash-table",
"string",
"greedy",
"sorting",
"counting"
] | "{\"lang\": \"cpp\", \"distribution\": [[\"4\", 0.023399999999999997], [\"6\", 0.0109], [\"8\", 0.01(...TRUNCATED) | "{\"lang\": \"cpp\", \"distribution\": [[\"8530\", 0.0265], [\"8790\", 0.0031], [\"9050\", 0.0047], (...TRUNCATED) | [{"code":"struct Solution {\n int minimumPushes(string_view input) {\n int i = 0;\n (...TRUNCATED) | 104 | [{"code":"struct Solution {\n int minimumPushes(string_view input) {\n int i;\n int(...TRUNCATED) | 80 |
2,076 | sum-of-digits-of-string-after-convert | "<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <c(...TRUNCATED) | 74.965412 | Easy | [
"string",
"simulation"
] | "{\"lang\": \"cpp\", \"distribution\": [[\"0\", 41.3082], [\"1\", 0.024], [\"2\", 3.8779], [\"3\", 1(...TRUNCATED) | "{\"lang\": \"cpp\", \"distribution\": [[\"7500\", 0.0017], [\"7600\", 0.1253], [\"7700\", 2.0247], (...TRUNCATED) | [{"code":"class Solution {\npublic:\n int getLucky(string s, int k) {\n int len = s.length(...TRUNCATED) | 2 | [{"code":"int sumDigits(int num) {\n int sum = 0;\n while (num) {\n sum += num % 10;\n (...TRUNCATED) | 45 |
2,251 | number-of-ways-to-divide-a-long-corridor | "<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <(...TRUNCATED) | 49.341445 | Hard | [
"math",
"string",
"dynamic-programming"
] | "{\"lang\": \"cpp\", \"distribution\": [[\"51\", 1.1365], [\"64\", 1.1365], [\"76\", 0.2273], [\"89\(...TRUNCATED) | "{\"lang\": \"cpp\", \"distribution\": [[\"28675\", 1.1362999999999999], [\"33425\", 39.545500000000(...TRUNCATED) | [{"code":"class Solution {\npublic:\n int numberOfWays(string corridor) {\n ios_base::sync(...TRUNCATED) | 74 | [{"code":"class Solution {\npublic:\n int numberOfWays(string_view corridor) {\n constexpr(...TRUNCATED) | 43 |
2,271 | rearrange-array-elements-by-sign | "<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</stro(...TRUNCATED) | 84.072132 | Medium | [
"array",
"two-pointers",
"simulation"
] | "{\"lang\": \"cpp\", \"distribution\": [[\"61\", 0.008], [\"62\", 0.004], [\"64\", 0.004], [\"65\", (...TRUNCATED) | "{\"lang\": \"cpp\", \"distribution\": [[\"124617\", 0.064], [\"124852\", 0.0721], [\"125087\", 0.26(...TRUNCATED) | [{"code":"class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios(...TRUNCATED) | 120 | [{"code":"class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n st(...TRUNCATED) | 108 |
2,519 | find-the-original-array-of-prefix-xor | "<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find a(...TRUNCATED) | 88.04974 | Medium | [
"array",
"bit-manipulation"
] | "{\"lang\": \"cpp\", \"distribution\": [[\"30\", 0.0437], [\"31\", 0.0437], [\"33\", 0.0437], [\"34\(...TRUNCATED) | "{\"lang\": \"cpp\", \"distribution\": [[\"78300\", 0.3493], [\"78400\", 1.5721], [\"78500\", 8.821](...TRUNCATED) | [{"code":"class Solution {\npublic:\n vector<int> findArray(vector<int>& v) {\n ios::sync_(...TRUNCATED) | 65 | [{"code":"#pragma GCC optimize(\"Ofast\", \"inline\", \"unroll-loops\")\nclass Solution {\npublic:\n(...TRUNCATED) | 19 |
End of preview. Expand
in Dataset Viewer.
README.md exists but content is empty.
Use the Edit dataset card button to edit it.
- Downloads last month
- 31