id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 1 | {
"code": "class DisjointSet{\n vector<int>rank,par,size;\npublic:\n DisjointSet(int n)\n {\n rank.resize(n+1,0);\n size.resize(n+1);\n par.resize(n+1);\n for(int i=0;i<=n;i++)\n {\n par[i]=i;\n size[i]=1;\n }\n }\n\n int findPar(int u)\n {\n if(u==par[u])\n return u;\n\n return par[u] = findPar(par[u]);\n }\n\n void unionByRank(int u, int v)\n {\n int ult_pu=findPar(u);\n int ult_pv=findPar(v);\n if(ult_pu==ult_pv) return;\n\n if(rank[ult_pu]<rank[ult_pv])\n par[ult_pu]=ult_pv;\n\n else if(rank[ult_pv]<rank[ult_pv])\n par[ult_pv]=ult_pu;\n\n else\n {\n par[ult_pu]=ult_pv;\n rank[ult_pv]++;\n } \n }\n\n void unionBySize(int u, int v)\n {\n int ult_pu=findPar(u);\n int ult_pv=findPar(v);\n if(ult_pu==ult_pv) return;\n\n if(size[ult_pu]<ult_pv)\n {\n par[ult_pu]=ult_pv;\n size[ult_pv]+=size[ult_pu];\n }\n else\n {\n par[ult_pv]=ult_pu;\n size[ult_pu]+=size[ult_pv];\n }\n\n }\n};\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.size();\n\n DisjointSet ds(n);\n for(int i=0;i<pairs.size();i++)\n ds.unionByRank(pairs[i][0],pairs[i][1]);\n\n unordered_map<int, multiset<char>>m;\n for(int i=0;i<n;i++)\n {\n m[ds.findPar(i)].insert(s[i]);\n }\n\n for(int i=0;i<n;i++)\n {\n auto it=m[ds.findPar(i)].begin();\n s[i]=*it;\n m[ds.findPar(i)].erase(it);\n } \n return s;\n \n }\n};",
"memory": "60012"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 1 | {
"code": "class DisjointSet{\n vector<int>rank,par,size;\npublic:\n DisjointSet(int n)\n {\n rank.resize(n+1,0);\n size.resize(n+1);\n par.resize(n+1);\n for(int i=0;i<=n;i++)\n {\n par[i]=i;\n size[i]=1;\n }\n }\n\n int findPar(int u)\n {\n if(u==par[u])\n return u;\n\n return par[u] = findPar(par[u]);\n }\n\n void unionByRank(int u, int v)\n {\n int ult_pu=findPar(u);\n int ult_pv=findPar(v);\n if(ult_pu==ult_pv) return;\n\n if(rank[ult_pu]<rank[ult_pv])\n par[ult_pu]=ult_pv;\n\n else if(rank[ult_pv]<rank[ult_pv])\n par[ult_pv]=ult_pu;\n\n else\n {\n par[ult_pu]=ult_pv;\n rank[ult_pv]++;\n } \n }\n\n void unionBySize(int u, int v)\n {\n int ult_pu=findPar(u);\n int ult_pv=findPar(v);\n if(ult_pu==ult_pv) return;\n\n if(size[ult_pu]<ult_pv)\n {\n par[ult_pu]=ult_pv;\n size[ult_pv]+=size[ult_pu];\n }\n else\n {\n par[ult_pv]=ult_pu;\n size[ult_pu]+=size[ult_pv];\n }\n\n }\n};\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.size();\n\n DisjointSet ds(n);\n for(int i=0;i<pairs.size();i++)\n ds.unionBySize(pairs[i][0],pairs[i][1]);\n\n unordered_map<int, multiset<char>>m;\n for(int i=0;i<n;i++)\n {\n m[ds.findPar(i)].insert(s[i]);\n }\n\n for(int i=0;i<n;i++)\n {\n auto it=m[ds.findPar(i)].begin();\n s[i]=*it;\n m[ds.findPar(i)].erase(it);\n } \n return s;\n \n }\n};",
"memory": "60012"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n const int SIZE = s.size();\n UnionFind disjointSet(SIZE);\n \n for (vector<int> pair : pairs)\n disjointSet.quickUnion(pair[0], pair[1]);\n \n for (int i = 0; i < SIZE; i++)\n disjointSet.find(i);\n \n unordered_map<int, vector<int>> disjointSetDic;\n unordered_map<int, string> subStringDic;\n\n for (int i = 0; i < SIZE; i++)\n {\n disjointSetDic[disjointSet.root[i]].push_back(i);\n subStringDic[disjointSet.root[i]] += s[i];\n }\n \n for (pair<const int, string>& pair : subStringDic)\n {\n sort(pair.second.begin(), pair.second.end());\n }\n \n for (pair<int, vector<int>> pair : disjointSetDic)\n {\n int size = pair.second.size();\n for (int i = 0; i < size; i++)\n {\n s[pair.second[i]] = subStringDic[pair.first][i];\n }\n }\n return s;\n };\nprivate:\n class UnionFind{\n public:\n vector<int> root;\n UnionFind(int size): root(size), rank(size)\n {\n for (int i = 0; i < size; i++)\n {\n root[i] = i;\n rank[i] = 1;\n }\n }\n int find(int vertex)\n {\n if (vertex == root[vertex])\n return vertex;\n return root[vertex] = find(root[vertex]);\n }\n void quickUnion(int vertexA, int vertexB)\n {\n int rootA = find(vertexA);\n int rootB = find(vertexB);\n if (rootA != rootB)\n {\n if (rank[rootA] > rank[rootB])\n root[rootB] = rootA;\n else if (rank[rootA] < rank[rootB])\n root[rootA] = rootB;\n else\n {\n root[rootB] = rootA;\n rank[rootA]++;\n }\n }\n }\n private:\n vector<int> rank;\n };\n};",
"memory": "60537"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 1 | {
"code": "class UnionFind {\nprivate:\n vector<int> root;\n vector<int> rank;\npublic:\n // Initialize the array root and rank\n // Each vertex is representative of itself with rank 1\n UnionFind(int sz) : root(sz), rank(sz) {\n for (int i = 0; i < sz; i++) {\n root[i] = i;\n rank[i] = 1;\n }\n }\n \n // Get the root of a vertex\n int find(int x) {\n if (x == root[x]) {\n return x;\n }\n return root[x] = find(root[x]);\n }\n\n // Perform the union of two components\n void unionSet(int x, int y) {\n int rootX = find(x);\n int rootY = find(y);\n if (rootX != rootY) {\n if (rank[rootX] >= rank[rootY]) {\n root[rootY] = rootX;\n rank[rootX] += rank[rootY];\n } else {\n root[rootX] = rootY;\n rank[rootY] += rank[rootX];\n }\n }\n }\n};\n\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n UnionFind uf(s.size());\n \n // Iterate over the edges\n for (vector<int> edge : pairs) {\n int source = edge[0];\n int destination = edge[1];\n \n // Perform the union of end points\n uf.unionSet(source, destination);\n }\n \n \n unordered_map<int, vector<int>> rootToComponent;\n // Group the vertices that are in the same component\n for (int vertex = 0; vertex < s.size(); vertex++) {\n int root = uf.find(vertex);\n // Add the vertices corresponding to the component root\n rootToComponent[root].push_back(vertex);\n }\n \n // String to store the answer\n string smallestString(s.length(), ' ');\n // Iterate over each component\n for (auto component : rootToComponent) {\n vector<int> indices = component.second;\n \n // Sort the characters in the group\n vector<char> characters;\n for (int index : indices) {\n characters.push_back(s[index]);\n }\n sort(characters.begin(), characters.end());\n \n // Store the sorted characters\n for (int index = 0; index < indices.size(); index++) {\n smallestString[indices[index]] = characters[index];\n }\n }\n \n return smallestString;\n }\n};\n\n\n\n// class Solution {\n// public:\n// // Maximum number of vertices\n// static const int N = 100001;\n// vector<int> adj[N];\n// bool visited[N];\n \n// void DFS(string& s, int vertex, vector<char>& characters, vector<int>& indices) {\n// // Add the character and index to the list\n// characters.push_back(s[vertex]);\n// indices.push_back(vertex);\n// visited[vertex] = true;\n \n// // Traverse the adjacents\n// for (int adjacent : adj[vertex]) {\n// if (!visited[adjacent]) {\n// DFS(s, adjacent, characters, indices);\n// }\n// }\n// }\n \n// string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n \n// // Build the adjacency list\n// for (vector<int> edge : pairs) {\n// int source = edge[0], destination = edge[1];\n// // Undirected edge\n// adj[source].push_back(destination);\n// adj[destination].push_back(source);\n// }\n \n// for (int vertex = 0; vertex < s.size(); vertex++) {\n// // If not covered in the DFS yet\n// if (!visited[vertex]) {\n// vector<char> characters;\n// vector<int> indices;\n \n// // DFS, get all vertices that are connected with curr vertex\n// DFS(s, vertex, characters, indices);\n// // Sort the list of characters and indices\n// sort(characters.begin(), characters.end());\n// sort(indices.begin(), indices.end());\n\n// // Store the sorted characters corresponding to the index\n// for (int index = 0; index < characters.size(); index++) {\n// s[indices[index]] = characters[index];\n// }\n// }\n// }\n// return s;\n// }\n// };\n\n\n\n\n\n\n\n\n\n\n\n\n// ////////\n\n\n// //////\n\n\n",
"memory": "60537"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 1 | {
"code": "class UnionFind {\nprivate:\n vector<int> root;\n vector<int> rank;\npublic:\n // Initialize the array root and rank\n // Each vertex is representative of itself with rank 1\n UnionFind(int sz) : root(sz), rank(sz) {\n for (int i = 0; i < sz; i++) {\n root[i] = i;\n rank[i] = 1;\n }\n }\n \n // Get the root of a vertex\n int find(int x) {\n if (x == root[x]) {\n return x;\n }\n return root[x] = find(root[x]);\n }\n\n // Perform the union of two components\n void unionSet(int x, int y) {\n int rootX = find(x);\n int rootY = find(y);\n if (rootX != rootY) {\n if (rank[rootX] >= rank[rootY]) {\n root[rootY] = rootX;\n rank[rootX] += rank[rootY];\n } else {\n root[rootX] = rootY;\n rank[rootY] += rank[rootX];\n }\n }\n }\n};\n\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n UnionFind uf(s.size());\n \n // Iterate over the edges\n for (vector<int> edge : pairs) {\n int source = edge[0];\n int destination = edge[1];\n \n // Perform the union of end points\n uf.unionSet(source, destination);\n }\n \n \n unordered_map<int, vector<int>> rootToComponent;\n // Group the vertices that are in the same component\n for (int vertex = 0; vertex < s.size(); vertex++) {\n int root = uf.find(vertex);\n // Add the vertices corresponding to the component root\n rootToComponent[root].push_back(vertex);\n }\n \n // String to store the answer\n string smallestString(s.length(), ' ');\n // Iterate over each component\n for (auto component : rootToComponent) {\n vector<int> indices = component.second;\n \n // Sort the characters in the group\n vector<char> characters;\n for (int index : indices) {\n characters.push_back(s[index]);\n }\n sort(characters.begin(), characters.end());\n \n // Store the sorted characters\n for (int index = 0; index < indices.size(); index++) {\n smallestString[indices[index]] = characters[index];\n }\n }\n \n return smallestString;\n }\n};",
"memory": "61062"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 1 | {
"code": "class UnionFind {\nprivate:\n vector<int> root;\n vector<int> rank;\npublic:\n // Initialize the array root and rank\n // Each vertex is representative of itself with rank 1\n UnionFind(int sz) : root(sz), rank(sz) {\n for (int i = 0; i < sz; i++) {\n root[i] = i;\n rank[i] = 1;\n }\n }\n \n // Get the root of a vertex\n int find(int x) {\n if (x == root[x]) {\n return x;\n }\n return root[x] = find(root[x]);\n }\n\n // Perform the union of two components\n void unionSet(int x, int y) {\n int rootX = find(x);\n int rootY = find(y);\n if (rootX != rootY) {\n if (rank[rootX] >= rank[rootY]) {\n root[rootY] = rootX;\n rank[rootX] += rank[rootY];\n } else {\n root[rootX] = rootY;\n rank[rootY] += rank[rootX];\n }\n }\n }\n};\n\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n UnionFind uf(s.size());\n \n // Iterate over the edges\n for (vector<int> edge : pairs) {\n int source = edge[0];\n int destination = edge[1];\n \n // Perform the union of end points\n uf.unionSet(source, destination);\n }\n \n \n unordered_map<int, vector<int>> rootToComponent;\n // Group the vertices that are in the same component\n for (int vertex = 0; vertex < s.size(); vertex++) {\n int root = uf.find(vertex);\n // Add the vertices corresponding to the component root\n rootToComponent[root].push_back(vertex);\n }\n \n // String to store the answer\n string smallestString(s.length(), ' ');\n // Iterate over each component\n for (auto component : rootToComponent) {\n vector<int> indices = component.second;\n \n // Sort the characters in the group\n vector<char> characters;\n for (int index : indices) {\n characters.push_back(s[index]);\n }\n sort(characters.begin(), characters.end());\n \n // Store the sorted characters\n for (int index = 0; index < indices.size(); index++) {\n smallestString[indices[index]] = characters[index];\n }\n }\n \n return smallestString;\n }\n};",
"memory": "61062"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\n\nprivate:\n int set_or_recur_parent(vector<int>& parent,int u){\n if(parent[u]==-1 || parent[u] == u ){\n return u;\n }\n return parent[u]=set_or_recur_parent(parent,parent[u]);\n }\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n //build a data struct for union\n int size = s.size();\n vector<int> parent(size,-1);\n for(auto e: pairs){\n int v = e[0];\n int u = e[1];\n int v_p = set_or_recur_parent(parent,v);\n int u_p = set_or_recur_parent(parent,u);\n // int min_ = min(min(v_p,u_p),min(u,v));\n // parent[v] = min_;\n // parent[u] = min_;\n if(v_p!=u_p){\n parent[u_p]=v;\n }\n }\n map<int,vector<int>> char_sets;\n for(int i=0;i<size;i++){\n int group_id = set_or_recur_parent(parent,i);\n if(group_id!=-1){\n char_sets[group_id].emplace_back(i);\n }\n }\n for(auto& pair: char_sets){\n vector<int> char_index = pair.second;\n // vector<std::pair<char,int>> char_id_pair_list;\n vector<char> char_list;\n vector<int> id_list;\n for(int id: char_index){\n cout<<id<<\" \\n\";\n char_list.emplace_back(s[id]);\n id_list.emplace_back(id);\n }\n auto cmp = [](std::pair<char,int> a, std::pair<char,int> b){\n return a.first>b.first;\n };\n // std::sort(char_id_pair_list.begin(),char_id_pair_list.end(),cmp);\n std::sort(char_list.begin(),char_list.end());\n std::sort(id_list.begin(),id_list.end());\n //update the original string\n for(int i=0;i<id_list.size();i++){\n s[id_list[i]] = char_list[i];\n }\n }\n return s;\n }\n};",
"memory": "61587"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n string ans(s.size(), '*');\n\n UnionFind uF(s.size());\n for(auto pair : pairs) {\n uF.unionSet(pair[0], pair[1]);\n }\n\n for(auto u : uF.getUnions()) {\n string tmp;\n for(auto i : u.second) {\n tmp += s[i];\n }\n sort(tmp.begin(), tmp.end());\n \n int j = 0;\n for(auto i : u.second) {\n ans[i] = tmp[j++];\n }\n }\n\n return ans;\n }\n\nprivate:\n class UnionFind {\n public:\n UnionFind(int sz) : root(sz), rank(sz, 1) {\n for(int i=0; i<sz; i++) {\n root[i] = i;\n }\n }\n\n int find(int x) {\n if(x == root[x]) {\n return x;\n }\n return root[x] = find(root[x]);\n }\n\n void unionSet(int x, int y) {\n int rootX = find(x);\n int rootY = find(y);\n if(rootX == rootY) return;\n if(rank[rootX] > rank[rootY]) {\n root[rootY] = rootX;\n }\n else if(rank[rootX] < rank[rootY]) {\n root[rootX] = rootY;\n }\n else {\n root[rootX] = rootY;\n rank[rootY]++;\n }\n }\n\n unordered_map<int, vector<int>> getUnions() {\n if(unions.size()) return unions;\n for(int i=0; i<root.size(); i++) {\n find(i);\n unions[root[i]].push_back(i);\n }\n return unions;\n }\n\n private:\n vector<int> root;\n vector<int> rank;\n unordered_map<int, vector<int>> unions;\n };\n};",
"memory": "61587"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int find(int i,vector<int>&parent){\n if(i==parent[i])return i;\n return parent[i]=find(parent[i],parent);\n }\n void unio(int a,int b,vector<int>&parent,vector<int>&rank){\n int a_parent=find(a,parent);\n int b_parent=find(b,parent);\n if(a_parent==b_parent)return;\n if(rank[a_parent]<rank[b_parent]){\n parent[a_parent]=b_parent;\n }\n else if(rank[b_parent]<rank[a_parent]){\n parent[b_parent]=a_parent;\n }\n else{\n parent[a_parent]=b_parent;\n rank[a_parent]++; \n }\n\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.length();\n string ans=\"\";\n vector<int>parent(n,0);\n vector<int>rank(n,0);\n for(int i=0;i<n;i++){\n parent[i]=i;\n rank[i]=0;\n }\n for(auto i:pairs){\n if(find(i[0],parent)!=find(i[1],parent)){\n unio(i[0],i[1],parent,rank);\n }\n }\n unordered_map<int,vector<int>>m;\n unordered_map<int,string>ms;\n for(int i=0;i<n;i++){\n int p=find(i,parent);\n m[p].push_back(i);\n ms[p]+=s[i];\n }\n string res;\n for(int i=0;i<n;i++){\n res+='a';\n }\n for(auto i:m){\n int j=i.first;\n string x=ms[j];\n sort(x.begin(),x.end());\n vector<int>v=i.second;\n for(int k=0;k<v.size();k++){\n res[v[k]]=x[k];\n }\n }\n return res;}\n};",
"memory": "62112"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class UnionSet{\n public:\n UnionSet(int nodesCount) : islands(nodesCount) {\n rank = vector<int>(nodesCount, 1);\n root.resize(nodesCount);\n std::iota(root.begin(), root.end(), 0);\n }\n \n int FindRoot(int node){\n if (node == root[node])\n return root[node];\n return root[node] = FindRoot(root[node]);\n }\n \n void Union(int firstNode, int secondNode){\n auto firstRoot = FindRoot(firstNode);\n auto secondRoot = FindRoot(secondNode);\n \n if (firstRoot != secondRoot){\n if (rank[firstRoot] > rank[secondRoot]){\n root[secondRoot] = firstRoot;\n }\n else if (rank[firstRoot] < rank[secondRoot]){\n root[firstRoot] = secondRoot;\n }\n else{\n root[secondRoot] = firstRoot;\n ++rank[firstRoot];\n }\n --islands;\n }\n }\n \n bool IsConnected(int left, int right){\n return FindRoot(left) == FindRoot(right);\n }\n \n int Islands(){\n return islands;\n }\n \n std::unordered_map<int, set<int>> GetIsLands(){\n std::unordered_map<int, set<int>> islands;\n for (int i = 0; i < root.size(); ++i){\n islands[FindRoot(i)].insert(i);\n }\n return islands;\n }\n \n\n int islands = 0;\n vector<int> rank;\n vector<int> root;\n \n};\n\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n string answer;\n answer.resize(s.size());\n UnionSet disjointSet(s.size());\n for (const auto& elem : pairs){\n disjointSet.Union(elem[0], elem[1]);\n }\n auto islands = disjointSet.GetIsLands();\n for (const auto& [key, value] : islands){\n vector<int> sortedIndexes(value.begin(), value.end());\n vector<int> sortedLetters(value.begin(), value.end());\n std::sort(sortedLetters.begin(), sortedLetters.end(), [&s] (int left, int right){\n return s[left] < s[right];\n });\n for (int i = 0; i < sortedIndexes.size(); ++i){\n answer[sortedIndexes[i]] = s[sortedLetters[i]];\n }\n }\n return answer;\n }\n};",
"memory": "62112"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int>root;\n vector<int>rank;\n int find(int x){\n if(x == root[x])\n return x;\n root[x] = find(root[x]);\n return root[x];\n }\n void unionPair(int x, int y){\n int rootX = find(x);\n int rootY = find(y);\n \n if(rootX != rootY){\n if(rank[rootX] > rank[rootY]){\n root[rootY] = rootX;\n }\n else if(rank[rootY] < rank[rootX]){\n root[rootX] = rootY;\n }\n else{\n root[rootY] = rootX;\n rank[rootX]++;\n }\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.length();\n for(int i = 0; i < n; i++ ){\n root.push_back(i);\n rank.push_back(1);\n }\n \n for(auto pair : pairs){\n unionPair(pair[0], pair[1]);\n }\n\n map<int, vector<int>> roottoComp;\n\n for(int vertex = 0; vertex < n; vertex++){\n int root = find(vertex);\n roottoComp[root].push_back(vertex);\n }\n\n string output(n, ' ');\n\n for(auto component : roottoComp ){\n vector<int> indices = component.second;\n\n vector<char> chars;\n for(int idx : indices){\n chars.push_back(s[idx]);\n }\n sort(chars.begin(), chars.end());\n\n for(int idx = 0; idx < indices.size(); idx++){\n output[indices[idx]] = chars[idx];\n }\n }\n\n return output;\n }\n};",
"memory": "62637"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class DSU{\npublic:\n vector<int>parent;\n vector<int>size;\n DSU(int n){\n parent.resize(n);\n size.resize(n,1);\n for(int i=0;i<n;i++)\n parent[i]=i;\n }\n int find(int u){\n if(u==parent[u])\n return u;\n return parent[u]=find(parent[u]);\n }\n void unite(int u,int v){\n int pu = find(u);\n int pv =find(v);\n if(pu==pv)\n return;\n if(pu<pv){\n parent[pv]=pu;\n size[pu]+=size[pv];\n }\n else\n {\n parent[pu]=pv;\n size[pv]+=size[pu];\n }\n }\n};\n\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n =s.length();\n DSU ds(n);\n for(int i=0;i<pairs.size();i++)\n ds.unite(pairs[i][0],pairs[i][1]);\n map<int,vector<int>>mp;\n for(int i=0;i<n;i++){\n int p =ds.find(i);\n // if(p==i)\n // continue;\n mp[p].push_back(i);\n }\n for(auto it:mp){\n multiset<int>st;\n // st.insert(it.first);\n for(auto itt:it.second){\n // cout<<itt<<\" \";\n st.insert(s[itt]);\n }\n auto x=st.begin();\n for(auto itt:it.second)\n {\n s[itt]=*x;\n x++;\n }\n st.clear();\n // cout<<endl;\n }\n return s;\n }\n};",
"memory": "62637"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class DSU{\n public:\n #define vi vector<int>\n vi par,size;\n DSU(int n){\n par.resize(n+1);\n size.resize(n+1,1);\n for(int i=0;i<=n;i++)par[i]=i;\n }\n int f_up(int u){\n if(par[u]==u)return par[u];\n return par[u]=f_up(par[u]);\n }\n void ubs(int u,int v){\n int pu=f_up(u),pv=f_up(v);\n if(pu==pv)return;\n if(size[pu]>size[pv]){\n size[pu]+=size[pv];\n par[pv]=pu;\n }else{\n size[pv]+=size[pu];\n par[pu]=pv;\n }\n }\n};\nclass Solution {\npublic:\n #define vi vector<int>\n #define vvi vector<vi>\n #define vc vector<char>\n #define pb push_back\n #define ff first\n #define ss second\n string smallestStringWithSwaps(string s, vvi &v) {\n int n=s.size();\n DSU ds(n);\n for(auto i:v){\n ds.ubs(i[0],i[1]);\n }\n map<int,vi>idx_mp;\n map<int,string>str_mp;\n for(int i=0;i<n;i++){\n int fp=ds.f_up(i);\n idx_mp[fp].pb(i);\n str_mp[fp]+=s[i];\n }\n for(auto &i:str_mp){\n sort(i.ss.begin(),i.ss.end());\n }\n vc ans(n);\n for(auto i:idx_mp){\n int j=i.ff; \n vi idx=i.ss;\n for(int k=0;k<idx.size();k++)ans[idx[k]]=str_mp[j][k];\n }\n string p;\n for(auto i:ans)p+=i;\n return p;\n }\n};",
"memory": "63162"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "\nclass dsu{\n public:\n vector<int>parent,size;\n dsu(int n){\n parent.resize(n);\n size.resize(n);\n for(int i=0;i<n;i++){\n parent[i]=i;\n size[i]=1;\n }\n }\n int find(int u){\n if(parent[u]==u)return u;\n return parent[u]=find(parent[u]);\n }\n void Union(int u,int v){\n int p=find(u);\n int q=find(v);\n if(p==q)return;\n if(size[p]<=size[q]){\n parent[p]=q;\n size[q]+=size[p];\n }\n else{\n parent[q]=p;\n size[p]+=size[q];\n }\n }\n};\n\n\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n for(auto i:pairs){\n int u=i[0],v=i[1];\n if(u>v)swap(u,v);\n }\n sort(pairs.begin(),pairs.end());\n\n int n=s.size();\n dsu ds(n);\n for(auto i:pairs){\n int u=i[0],v=i[1];\n ds.Union(u,v);\n }\n vector<vector<int>>v(n);\n for(int i=0;i<n;i++)v[ds.find(i)].push_back(i);\n \n for(int i=0;i<n;i++){\n if(v[i].size()==0)continue;\n sort(v[i].begin(),v[i].end());\n string r;\n for(int j=0;j<v[i].size();j++){\n r.push_back(s[v[i][j]]);\n }\n sort(r.begin(),r.end());\n for(int j=0;j<v[i].size();j++){\n s[v[i][j]]=r[j];\n }\n }\n return s;\n }\n \n};",
"memory": "63162"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class DisjointSet\n{\npublic:\n vector<int> parent, size;\n DisjointSet(int n)\n {\n parent.resize(n+1);\n size.resize(n+1);\n for(int i=0; i<=n; i++) parent[i]=i;\n }\n int findParent(int u)\n {\n if(parent[u]==u) return u;\n return parent[u]=findParent(parent[u]);\n }\n void unionBySize(int u, int v)\n {\n int upu = findParent(u);\n int upv = findParent(v);\n int su = size[upu], sv = size[upv];\n if(su < sv)\n {\n parent[upu] = upv;\n size[upv] += size[upu];\n }\n else\n {\n parent[upv] = upu;\n size[upu] += size[upv];\n }\n }\n};\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n DisjointSet dsu(n);\n for(auto it: pairs)\n {\n int u = it[0];\n int v = it[1];\n dsu.unionBySize(u,v);\n }\n vector<vector<int>> groups(n+1);\n for(int i=0; i<n; i++)\n {\n int group = dsu.findParent(i);\n groups[group].push_back(i);\n }\n vector<vector<char>> cgroups(n+1);\n for(int j=0; j<groups.size(); j++)\n {\n int m = groups[j].size();\n vector<char> temp;\n for(int i=0; i<m; i++)\n {\n temp.push_back(s[groups[j][i]]);\n }\n sort(temp.begin(), temp.end(), greater<>());\n cgroups[j] = temp;\n }\n string ans=\"\";\n for(int i=0; i<n; i++)\n {\n int group = dsu.findParent(i);\n char curr = cgroups[group].back();\n cgroups[group].pop_back();\n ans += curr;\n }\n return ans;\n }\n};",
"memory": "63687"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n UnionSet groups(s.size());\n for (int i = 0 ; i < pairs.size() ; i++)\n {\n groups.connect(pairs[i][0], pairs[i][1]);\n }\n \n unordered_map<int, vector<int>> classification;\n for (int i = 0; i < s.size(); i++)\n {\n int root = groups.find(i);\n classification[root].push_back(i);\n }\n \n string ret(s.size(), 'a');\n Comparer comparer(s);\n for (int i = 0; i < classification.size(); i++)\n {\n vector<int> indices(classification[i]);\n sort(indices.begin(), indices.end());\n \n sort(classification[i].begin(), classification[i].end(), comparer);\n for (int j = 0; j < classification[i].size(); j++)\n {\n ret[indices[j]] = s[classification[i][j]];\n }\n }\n \n return ret;\n }\n\nprivate:\n \n class Comparer\n {\n private:\n string_view sv;\n \n public:\n Comparer(const string& s) : sv(s)\n {\n \n }\n\n bool operator()(const int& i, const int& j)\n {\n return sv[i] < sv[j];\n }\n };\n\nclass UnionSet{\n\n public:\n UnionSet(int size): rank(size,0), root(size,0), count(size)\n {\n for (int i = 0 ; i < size; i++)\n {\n root[i] = i;\n }\n }\n \n int find(int i)\n {\n if (i == root[i])\n return i;\n \n root[i] = find(root[i]);\n return root[i];\n }\n \n int getCount()\n {\n return count;\n }\n \n bool connect(int i, int j)\n {\n int rootI = find(i);\n int rootJ = find(j);\n if (rootI == rootJ)\n return false;\n \n if (rank[rootI] > rank[rootJ])\n root[rootJ] = rootI;\n else if (rank[rootJ] > rank[rootI])\n root[rootI] = rootJ;\n else\n {\n root[rootI] = rootJ;\n rank[rootI]++;\n }\n \n count--;\n return true;\n }\n \n private:\n vector<int> rank;\n vector<int> root;\n int count;\n };\n};",
"memory": "63687"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Disjoint{\n\n public :\n vector<int>parent;\n vector<int>size;\n\n Disjoint(int n){\n parent.resize(n+1);\n size.resize(n+1);\n for(int i=0;i<n+1;i++){\n parent[i]=i;\n size[i]=1;\n }\n }\n\n int find(int u){\n if(u==parent[u]) {\n return u;\n }\n return parent[u]=find(parent[u]);\n }\n\n void unite(int u,int v){\n int upar=find(u);\n int vpar=find(v);\n\n if(upar==vpar) return ;\n if(size[upar] >=size[vpar]){\n parent[vpar]=upar;\n size[upar]+=size[vpar];\n }\n else{\n parent[upar]=vpar;\n size[vpar]+=size[upar];\n }\n }\n};\n\n\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n\n int n=s.length();\n Disjoint ds(n);\n\n for(auto it:pairs){\n ds.unite(it[0],it[1]);\n }\n \n unordered_map<int,vector<int>>mp;\n vector<int>com;\n for(int i=0;i<n;i++){\n int upar=ds.find(i);\n mp[upar].push_back(i);\n }\n unordered_map<int,string>mp2;\n for(auto it:mp){\n vector<int>vect=it.second;\n sort(it.second.begin(),it.second.end());\n string str=\"\";\n for(auto ind:vect){\n str.push_back(s[ind]);\n }\n sort(str.begin(),str.end());\n mp2[it.first]=str;\n }\n for(auto it:mp){\n vector<int>vect=it.second;\n string str=mp2[it.first];\n int i=0;\n for(auto ind:vect){\n s[ind]=str[i];\n i++;\n }\n }\n return s;\n }\n};",
"memory": "64212"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class UnionFind{\n public : \n vector<int> par, sz;\n UnionFind(int n): par(n) , sz(n,1) {\n iota(par.begin(),par.end(),0);\n }\n int find(int x){\n if(par[x]==x) return x;\n return par[x] = find(par[x]);\n }\n \n bool Union(int x,int y){\n int xp = find(x);\n int yp = find(y);\n \n if(xp==yp) return false;\n \n if(sz[xp]>sz[yp]) {par[yp] = xp; sz[xp]+=sz[yp];}\n else {par[xp]=yp; sz[yp]+=sz[xp];}\n return true;\n }\n};\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n string ans(s.size(),' '); \n UnionFind uf(s.size()+1);\n for(auto x:pairs)\n uf.Union(x[0],x[1]);\n unordered_map<int,multiset<char>> m;\n for(int i=0;i<s.size();i++){\n m[uf.find(i)].insert(s[i]);\n } \n for(int i=0;i<s.size();i++){\n auto it = m[uf.find(i)].begin();\n ans[i]=*it;\n m[uf.find(i)].erase(it);\n } \n return ans;\n }\n};",
"memory": "64212"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> parent, rank;\n \n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n parent.resize(n), rank.resize(n, 1);\n for(int i=0; i<n; i++) {\n parent[i] = i;\n }\n \n for(auto pair : pairs) {\n unionFind(pair[0], pair[1]);\n }\n \n unordered_map<int, vector<int>> components;\n for(int i=0; i<n; i++) {\n components[find(i)].push_back(i);\n }\n \n unordered_map<int, vector<pair<char, int>>> sortedComponents;\n for(auto component : components) {\n for(auto element : component.second) {\n sortedComponents[component.first].push_back({s[element], element});\n }\n sort(components[component.first].begin(), components[component.first].end());\n sort(sortedComponents[component.first].begin(), sortedComponents[component.first].end());\n }\n \n for(auto component : components) {\n int n = component.second.size();\n for(int i=0; i<n; i++) {\n s[component.second[i]] = sortedComponents[component.first][i].first;\n }\n }\n return s;\n }\n \n int find(int x) {\n if(x == parent[x]) {\n return x;\n }\n return parent[x] = find(parent[x]);\n }\n \n bool unionFind(int x, int y) {\n int px = find(x), py = find(y);\n if(px == py) return false;\n \n if(rank[px] < rank[py]) {\n parent[px] = py;\n } else if(rank[px] > rank[py]) {\n parent[py] = px;\n } else {\n rank[py]++;\n parent[px] = py;\n }\n return true;\n }\n};\n",
"memory": "64737"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "using ll = long long;\nclass DSU{\n public:\n vector<int> rank;\n vector<int> parent;\n int n;\n DSU(int n){\n this->n=n;\n rank=vector<int>(n,1);\n parent=vector<int>(n,0);\n iota(parent.begin(),parent.end(),0);\n \n }\n ll find(ll a){\n if(parent[a]==a) return a;\n else return parent[a]=find(parent[a]);\n }\n void unionF(ll a , ll b ){\n ll pa = find(a);\n ll pb = find(b);\n if(pa==pb) return ;\n if(rank[pa]>rank[pb]){\n swap(pa,pb);\n }\n parent[pa]=pb;\n if (rank[pa] == rank[pb])\n rank[pb]++;\n }\n \n\n};\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n DSU ds(s.size());\n for(auto it:pairs){\n ds.unionF(it[0],it[1]);\n }\n map<int,multiset<char>> mp;\n for(int i=0;i<s.size();i++){\n mp[ds.find(i)].insert(s[i]);\n }\n for(int i=0;i<s.size();i++){\n auto it=mp[ds.find(i)].begin();\n s[i]=*it;\n // cout<<*it<<\" \";\n mp[ds.find(i)].erase(it);\n \n }\n return s;\n\n }\n};",
"memory": "64737"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class DSU{\npublic: \n vector<int> parent,size;\n DSU(int n){\n parent.resize(n,0);\n size.resize(n,1);\n for(int i=0;i<n;i++){\n parent[i]=i;\n }\n }\n int fup(int node){\n if(node==parent[node]){\n return node;\n }\n return parent[node]=fup(parent[node]);\n }\n void ubs(int u,int v){\n int ulu=fup(u),ulv=fup(v);\n if(ulu==ulv){\n return;\n }\n if(size[ulv]>size[ulu]){\n swap(ulu,ulv);\n }\n size[ulu]+=size[ulv];\n parent[ulv]=ulu;\n }\n};\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.size();\n DSU ds(n);\n for(auto it: pairs){\n int u=it[0],v=it[1];\n ds.ubs(u,v);\n }\n map<int,multiset<char>> m;\n for(int i=0;i<n;i++){\n m[ds.fup(i)].insert(s[i]);\n }\n string ans;\n for(int i=0;i<n;i++){\n int u=ds.fup(i);\n auto t=m[u].begin();\n ans.push_back(*t);\n m[u].erase(t);\n }\n return ans;\n }\n};",
"memory": "65262"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n map<int,multiset<char>>mp;\n\n int findp(int i,vector<int>&par){\n if(par[i]==i){\n return i;\n }\n return par[i]=findp(par[i],par);\n }\n void unionf(int a,int b,vector<int>&rank,vector<int>&par){\n\n a=findp(a,par);\n b=findp(b,par);\n //dsu union i s of 2 types \n //union by rank \n //and union by value\n //here we will be using union by rank\n\n if(a!=b){\n\n if(rank[a]<rank[b]){\n par[a]=b;\n\n\n }else if(rank[a]>rank[b]){\n par[b]=a;\n\n }else{\n par[a]=b;\n rank[b]++;\n }\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n\n\n\n //how the inution will go about\n\n int n=s.size();\n vector<int>rank(n,1);\n vector<int>par(n,0);\n for(int i=0;i<n;i++){\n rank[i]=1;\n par[i]=i;\n }\n\n for(auto e:pairs){\n int a=e[0];\n int b=e[1];\n unionf(a,b,rank,par);\n }\n\n //har index ke char ko apne connected componenet ke ultimate parent ware multiset me daal donga\n for(int i=0;i<s.size();i++){\n int p=findp(i,par);\n mp[p].insert(s[i]);\n }\n //here we will be applying union find on indexs which are connected ie \n //which can be swapped will be part of the same connected component\n\n\n //now i will try making the lexicographically smallest substring \n //from the data struture that i have made\n string ans=\"\";\n for(int i=0;i<s.size();i++){\n int p=findp(i,par);\n char c=*mp[p].begin();\n mp[p].erase(mp[p].begin());\n ans+=c;\n }\n return ans;\n\n\n\n\n\n //in this question we will be using the concept of dsu along wiht priority queue \n\n\n\n \n }\n};",
"memory": "65262"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool flag[100011];\n\n vector<int> vec[100001], pos;\n\n vector<char> allChars;\n\n string str;\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = pairs.size();\n\n for(int i=0;i<n;i++)flag[i] = false;\n\n str = s;\n\n for(int i=0;i<n;i++){\n\n int u = pairs[i][0];\n\n int v = pairs[i][1];\n\n \n\n vec[u].push_back(v);\n\n vec[v].push_back(u);\n\n }\n\n \n\n for(int i=0;i<n;i++){\n\n if(!flag[i]){\n\n pos.clear();\n\n allChars.clear();\n\n dfs(i);\n\n \n\n sort(allChars.begin(), allChars.end());\n\n sort(pos.begin(), pos.end());\n\n \n\n for(int j=0;j<pos.size();j++){\n\n str[ pos[j] ] = allChars[j];\n\n }\n\n }\n\n }\n\n \n\n return str;\n\n }\n\n \n\n void dfs(int nd){\n\n flag[nd] = true;\n\n allChars.push_back(str[nd]);\n\n pos.push_back(nd);\n\n \n\n for(auto v: vec[nd]){\n\n if(!flag[v]){\n\n dfs(v);\n\n }\n\n } \n }\n};",
"memory": "65787"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class DisjointSet\n{\nprivate:\n int size;\n vector<int> rank;\n vector<int> root;\npublic:\n DisjointSet(int nSize) :\n size(nSize),\n rank(nSize, 1)\n {\n for (int n = 0; n < nSize; n++)\n {\n root.push_back(n);\n }\n }\n \n int djsFind(int node)\n {\n vector<int> visited;\n while (node != root[node])\n {\n visited.push_back(node);\n node = root[node];\n }\n for (int n : visited)\n {\n root[n] = node;\n }\n return node;\n }\n \n void djsUnion(int left, int right)\n {\n int leftRoot = djsFind(left);\n int rightRoot = djsFind(right);\n \n if (leftRoot == rightRoot)\n {\n return;\n }\n else if (rank[leftRoot] > rank[rightRoot])\n {\n root[rightRoot] = leftRoot;\n size--;\n }\n else if (rank[rightRoot] > rank[leftRoot])\n {\n root[leftRoot] = rightRoot;\n size--;\n }\n else\n {\n root[leftRoot] = rightRoot;\n rank[rightRoot]++;\n size--;\n }\n }\n \n void getSets(unordered_map<int, vector<int>> & sets)\n {\n for (int i = 0; i < root.size(); i++)\n {\n int iRoot = djsFind(i);\n sets[iRoot].push_back(i);\n }\n }\n};\n\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n DisjointSet djs(s.length());\n for (vector<int> & p : pairs)\n {\n djs.djsUnion(p[0], p[1]);\n }\n \n unordered_map<int, vector<int>> sets;\n djs.getSets(sets);\n \n for (auto it = sets.begin(); it != sets.end(); it++)\n {\n vector<int> & nodes = it->second;\n \n sort(nodes.begin(), nodes.end());\n \n vector<char> chars;\n for (int n : nodes)\n {\n chars.push_back(s[n]);\n }\n sort(chars.begin(), chars.end());\n \n for (int i = 0; i < nodes.size(); i++)\n {\n s[nodes[i]] = chars[i];\n }\n }\n \n return s;\n }\n};",
"memory": "65787"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n vector<vector<int>> components;\n unordered_map<int, int> componentMap;\n for (auto pair: pairs) {\n if (pair[0] == pair[1]) {\n continue;\n }\n if (componentMap.find(pair[0]) == componentMap.end()) {\n if (componentMap.find(pair[1]) == componentMap.end()) {\n components.push_back({pair[0], pair[1]});\n componentMap[pair[0]] = components.size() - 1;\n componentMap[pair[1]] = components.size() - 1;\n } else {\n componentMap[pair[0]] = componentMap[pair[1]];\n components[componentMap[pair[1]]].push_back(pair[0]);\n }\n } else {\n if (componentMap.find(pair[1]) == componentMap.end()) {\n componentMap[pair[1]] = componentMap[pair[0]];\n components[componentMap[pair[0]]].push_back(pair[1]);\n } else {\n int comp0 = componentMap[pair[0]],\n comp1 = componentMap[pair[1]];\n vector<int> &component1 = components[comp1],\n &component0 = components[comp0];\n if (comp0 != comp1) {\n if (component1.size() > component0.size()) {\n component1.insert( component1.end(), component0.begin(), component0.end() );\n for (int pos: component0) {\n componentMap[pos] = comp1;\n }\n component0.clear();\n } else {\n component0.insert( component0.end(), component1.begin(), component1.end() );\n for (int pos: component1) {\n componentMap[pos] = comp0;\n }\n component1.clear();\n }\n }\n }\n }\n }\n\n for (auto component: components) {\n vector<char> letters(component.size());\n for (int i = 0; i < component.size(); i++) {\n letters[i] = s[component[i]];\n }\n sort(letters.begin(), letters.end());\n sort(component.begin(), component.end());\n for (int i = 0; i < component.size(); i++) {\n s[component[i]] = letters[i];\n }\n }\n return s;\n }\n};",
"memory": "66312"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int fn(int x, vector<int> &rn) {\n if(x==rn[x]) {\n return x;\n }\n return rn[x]=fn(rn[x],rn);\n }\n\n bool un(int x, int y, vector<int> &rn) {\n x=fn(x,rn);\n y=fn(y,rn);\n if(x==y) {\n return false;\n }\n rn[y]=x;\n return true;\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.size();\n vector<int> rn(n+1,0);\n for(int i=0; i<=n; i++) {\n rn[i]=i;\n }\n for(auto u:pairs) {\n un(u[0],u[1],rn);\n }\n vector<vector<int>> comps(n+1);\n for(int i=0; i<n; i++) {\n comps[fn(i,rn)].push_back(i);\n }\n string ans(n,'*');\n for(int i=0; i<n; i++) {\n if(!comps[i].empty()) {\n multiset<char> chars;\n for(auto v:comps[i]) {\n chars.insert(s[v]);\n }\n for(auto v:comps[i]) {\n ans[v]=*chars.begin();\n chars.erase(chars.begin());\n }\n }\n }\n return ans;\n }\n};",
"memory": "66837"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int fn(int x, vector<int> &rn) {\n if(x==rn[x]) {\n return x;\n }\n return rn[x]=fn(rn[x],rn);\n }\n\n bool un(int x, int y, vector<int> &rn) {\n x=fn(x,rn);\n y=fn(y,rn);\n if(x==y) {\n return false;\n }\n rn[y]=x;\n return true;\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.size();\n vector<int> rn(n+1,0);\n for(int i=0; i<=n; i++) {\n rn[i]=i;\n }\n for(auto u:pairs) {\n un(u[0],u[1],rn);\n }\n unordered_map<int,vector<int>> comps;\n for(int i=0; i<n; i++) {\n comps[fn(i,rn)].push_back(i);\n }\n string ans(n,'*');\n for(auto u:comps) {\n multiset<char> chars;\n for(auto v:u.second) {\n chars.insert(s[v]);\n }\n for(auto v:u.second) {\n ans[v]=*chars.begin();\n chars.erase(chars.begin());\n }\n }\n return ans;\n }\n};",
"memory": "66837"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class DisjointSet {\n public: \n vector<int> parent, size;\n DisjointSet(int n){\n parent.resize(n+1);\n size.resize(n+1);\n\n for (int i=0;i<=n;i++){\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n int findUpar(int node){\n if (parent[node] == node) return node;\n\n return parent[node] = findUpar(parent[node]);\n }\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\n if (size[ulp_u] < size[ulp_v]){\n size[ulp_v] += size[ulp_u];\n parent[ulp_u] = ulp_v; \n } else {\n size[ulp_u] += size[ulp_v];\n parent[ulp_v] = ulp_u; \n }\n }\n};\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.length();\n DisjointSet ds(n);\n unordered_map<int, vector<pair<char, int>>> adj;\n\n for (auto x: pairs){\n int u = x[0], v = x[1];\n ds.unionBySize(u, v);\n }\n\n vector<int> vis(n, 0);\n for (int i=0;i<n;i++){\n if (vis[i] == 0){\n int ulp = ds.findUpar(i);\n adj[ulp].push_back({s[i], i});\n }\n }\n \n vector<vector<pair<char, int>>> res;\n for (auto x: adj){\n vector<pair<char, int>> arr = x.second;\n sort(arr.begin(), arr.end());\n vector<int> indices;\n for (int i=0;i<arr.size();i++){\n indices.push_back(arr[i].second);\n }\n sort(indices.begin(), indices.end());\n\n for (int i=0;i<arr.size();i++){\n arr[i].second = indices[i];\n }\n res.push_back(arr);\n }\n\n vector<char> result(n);\n for (int i=0;i<res.size();i++){\n for (int j=0;j<res[i].size();j++){\n char ch = res[i][j].first;\n int ind = res[i][j].second;\n result[ind] = ch;\n }\n }\n \n string str = \"\";\n for (auto x: result){\n str += x;\n }\n return str;\n }\n};",
"memory": "67362"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\n void dfs(int node, vector<int> adj[], vector<int> &vis, vector<int> &component) {\n vis[node] = 1;\n component.push_back(node);\n for (auto it : adj[node]) {\n if (!vis[it]) {\n dfs(it, adj, vis, component);\n }\n }\n }\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.length();\n vector<int> adj[n];\n vector<int> vis(n, 0);\n\n // Build the adjacency list\n for (int i = 0; i < pairs.size(); i++) {\n int u = pairs[i][0];\n int v = pairs[i][1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n // Traverse all nodes and apply DFS for connected components\n for (int i = 0; i < n; i++) {\n if (!vis[i]) {\n vector<int> component;\n dfs(i, adj, vis, component);\n\n // Sort the indices in the component and the corresponding characters\n sort(component.begin(), component.end());\n string temp = \"\";\n for (int idx : component) {\n temp += s[idx];\n }\n sort(temp.begin(), temp.end());\n\n // Assign sorted characters back to their original positions\n for (int j = 0; j < component.size(); j++) {\n s[component[j]] = temp[j];\n }\n }\n }\n\n return s;\n }\n};\n",
"memory": "67362"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void dfs(int s,vector<int>&vis,int n,vector<int>&arr,vector<int> adj[]){\n arr.push_back(s);\n vis[s]=1;\n for(auto &c:adj[s]){\n if(!vis[c]){\n dfs(c,vis,n,arr,adj);\n }\n }\n \n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n vector<int>adj[s.size()];\n for(auto &x:pairs){\n adj[x[0]].push_back(x[1]);\n adj[x[1]].push_back(x[0]);\n }\n vector<int>vis(s.size(),0);\n for(int i=0;i<s.size();i++){\n if(!vis[i]){\n vector<int>arr;\n dfs(i,vis,s.size(),arr,adj);\n sort(arr.begin(),arr.end());\n string str=\"\";\n for(auto &x:arr)str+=(s[x]);\n sort(str.begin(),str.end());\n int k=0;\n for(auto &x:arr)s[x]=str[k],k++;\n }\n }\n\n return s;\n }\n};",
"memory": "67887"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void dfs(int s,vector<int>&vis,int n,vector<int>&arr,vector<int> adj[]){\n arr.push_back(s);\n vis[s]=1;\n for(auto &c:adj[s]){\n if(!vis[c]){\n dfs(c,vis,n,arr,adj);\n }\n }\n \n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n vector<int>adj[s.size()];\n for(auto &x:pairs){\n adj[x[0]].push_back(x[1]);\n adj[x[1]].push_back(x[0]);\n }\n vector<int>vis(s.size(),0);\n for(int i=0;i<s.size();i++){\n if(!vis[i]){\n vector<int>arr;\n dfs(i,vis,s.size(),arr,adj);\n sort(arr.begin(),arr.end());\n string str=\"\";\n for(auto &x:arr)str+=(s[x]);\n sort(str.begin(),str.end());\n int k=0;\n for(auto &x:arr)s[x]=str[k],k++;\n }\n }\n\n return s;\n }\n};",
"memory": "67887"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void dfs(int start, vector<vector<int>> &adj, vector<int> &visited, int count)\n {\n if(visited[start] != 0)\n return;\n visited[start] = count;\n for(auto x: adj[start])\n {\n if(visited[x] == 0)\n dfs(x, adj, visited, count);\n }\n return;\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n vector<vector<int>> adj(s.length());\n for(auto p:pairs)\n {\n adj[p[0]].push_back(p[1]);\n adj[p[1]].push_back(p[0]);\n }\n vector<int> visited(s.length(), 0);\n int count = 1;\n for(int i = 0; i<s.length(); i++)\n {\n if(visited[i] != 0)\n continue;\n dfs(i, adj, visited, count);\n count++;\n }\n count--;\n // cout<<count<<endl;\n vector<string> temps(count, \"\");\n for(int i = 0;i<s.length(); i++)\n {\n temps[visited[i]-1] += s[i];\n }\n for(int i = 1; i<=count; i++)\n {\n sort(temps[i-1].begin(), temps[i-1].end());\n // cout<<temps[i-1]<<endl;\n }\n \n vector<int> indices(count, 0);\n string res = \"\";\n for(int i = 0; i<s.length(); i++)\n {\n res += temps[visited[i]-1][indices[visited[i]-1]];\n indices[visited[i]-1]++;\n }\n return res;\n }\n};",
"memory": "68412"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "typedef int const& T;\n\nclass Solution {\nprivate:\n std::vector<std::vector<int>> graph;\n std::vector<bool> visited;\n\n void add_undirected_edge(T from, T to) {\n graph[from].push_back(to);\n graph[to].push_back(from);\n }\n\n void dfs(T node, std::vector<int>& cc_indices) {\n visited[node] = true;\n cc_indices.push_back(node);\n\n for (auto const& neighbour : graph.at(node))\n if (!visited[neighbour])\n dfs(neighbour, cc_indices);\n }\n\npublic:\n // O(V) time and space\n std::string smallestStringWithSwaps(std::string str,\n std::vector<std::vector<int>>& pairs) {\n graph.resize(str.length()); // Adjacency List representaion\n visited.resize(str.length());\n\n // build graph\n for (auto const& edge : pairs)\n add_undirected_edge(edge[0], edge[1]);\n\n for (int i{}; i < static_cast<int>(str.length()); ++i)\n if (!visited[i]) // new CC\n {\n std::vector<int> cc_indices;\n dfs(i, cc_indices);\n\n // get letter's from cc of indices\n std::string cc_letters;\n for (auto const& idx : cc_indices)\n cc_letters += str[idx];\n\n std::sort(cc_indices.begin(), cc_indices.end());\n std::sort(cc_letters.begin(), cc_letters.end());\n\n // this wrong, don't do that\n // for (auto const &idx : cc_indices)\n // str[idx] = cc_letters[idx];\n\n for (int j{}; j < static_cast<int>(cc_indices.size()); ++j)\n str[cc_indices[j]] = cc_letters[j];\n }\n return str;\n }\n};",
"memory": "69987"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> indices; //Stores indices of same group.\n vector<bool> visited;\n vector<vector<int>> adjList;\n string indiceString; //Stores string formed by indices in the same group.\n void dfs(string &s,int n) //DFS to get all indices in same group.\n {\n visited[n]=true;\n indices.push_back(n);\n indiceString+=s[n];\n for(int &i:adjList[n])\n if(!visited[i])\n dfs(s,i);\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) \n {\n adjList.resize(s.length());\n visited.resize(s.length(),false);\n for(vector<int> &v:pairs) //Create adjacency list using the indice pairs\n adjList[v[0]].push_back(v[1]),adjList[v[1]].push_back(v[0]);\n for(int i=0;i<s.length();i++)\n if(!visited[i])\n {\n indiceString=\"\"; //Clear string formed by one group of indices before finding next group.\n indices.clear(); //Clear indices vector before finding another group.\n dfs(s,i);\n sort(indiceString.begin(),indiceString.end()); //Sort the characters in the same group.\n sort(indices.begin(),indices.end()); //Sort the indices in the same group. \n for(int i=0;i<indices.size();i++) //Replace all the indices in the same group with the sorted characters.\n s[indices[i]]=indiceString[i];\n }\n return s;\n }\n};",
"memory": "69987"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n void dfs(int node,string &s, vector<vector<int>> &adj, vector<int> &visited,vector<char> &temp,vector<int> &index){\n visited[node] = 1;\n temp.push_back(s[node]);\n index.push_back(node);\n\n for(auto adjNode : adj[node]){\n if(!visited[adjNode]){\n dfs(adjNode,s,adj,visited,temp,index);\n }\n }\n }\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.length();\n vector<vector<int>> adj(n);\n\n for(int i = 0;i<pairs.size();i++){\n adj[pairs[i][0]].push_back(pairs[i][1]);\n adj[pairs[i][1]].push_back(pairs[i][0]);\n }\n\n vector<int> visited(n,0);\n string ans(n,' ');\n for(int i = 0;i<n;i++){\n if(!visited[i]){\n vector<char> temp;\n vector<int> index;\n dfs(i,s,adj,visited,temp,index);\n sort(temp.begin(),temp.end());\n sort(index.begin(),index.end());\n\n for(int j = 0;j<index.size();j++){\n ans[index[j]] = temp[j];\n }\n }\n }\n\n return ans;\n }\n};",
"memory": "70512"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\n void dfs(int src,vector<vector<int>> &adjList,vector<int> &curr,vector<int> &vis){\n if(vis[src]) {\n return;\n }\n curr.push_back(src);\n vis[src]=true;\n for(auto& node:adjList[src]){\n dfs(node,adjList,curr,vis);\n }\n }\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n vector<int> vis(s.size(),0);\n vector<vector<int>> adjList(s.size());\n vector<vector<int>> v;\n for(auto& p:pairs){\n adjList[p[0]].push_back(p[1]);\n adjList[p[1]].push_back(p[0]);\n }\n for(int i=0;i<s.size();i++){\n if(!vis[i]){\n vector<int> curr;\n dfs(i,adjList,curr,vis);\n v.push_back(curr);\n }\n }\n for(auto& nums:v){\n sort(nums.begin(),nums.end());\n string curr=\"\";\n for(int num:nums) curr+=s[num];\n sort(curr.begin(),curr.end());\n for(int i=0;i<nums.size();i++) s[nums[i]]=curr[i];\n }\n return s;\n }\n};",
"memory": "70512"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void dfs(int node, vector<vector<int>> &graph, vector<int> &connectedComponent, vector<int> &visited) {\n visited[node] = 1;\n connectedComponent.push_back(node);\n for(int i = 0; i < graph[node].size(); i++) {\n if(visited[graph[node][i]] == 0) {\n dfs(graph[node][i], graph, connectedComponent, visited);\n }\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n vector<vector<int>> graph(s.size());\n for(int i = 0; i < pairs.size(); i++) {\n graph[pairs[i][0]].push_back(pairs[i][1]);\n graph[pairs[i][1]].push_back(pairs[i][0]);\n }\n vector<vector<int>> connectedComponents;\n vector<int> visited(s.size(), 0);\n for(int i = 0; i < s.size(); i++) {\n if(visited[i] == 0) {\n vector<int> cc;\n dfs(i, graph, cc, visited);\n connectedComponents.push_back(cc);\n }\n }\n vector<char> answer(s.size());\n for(int i = 0; i < connectedComponents.size(); i++) {\n string temp;\n sort(connectedComponents[i].begin(), connectedComponents[i].end());\n for(int j = 0; j < connectedComponents[i].size(); j++) {\n cout << connectedComponents[i][j] << \" \";\n temp += s[connectedComponents[i][j]];\n }\n cout << \"\\n\";\n sort(temp.begin(), temp.end());\n for(int j = 0; j < connectedComponents[i].size(); j++) {\n answer[connectedComponents[i][j]] = temp[j];\n }\n }\n\n string ans = \"\";\n for(int i = 0; i < answer.size(); i++) {\n ans += answer[i];\n }\n return ans;\n }\n};",
"memory": "71037"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n void dfs(int u,vector<int>adj[],vector<bool>&visited,string &s,vector<int>&index,string &str){\n visited[u]=true;\n\n index.push_back(u);\n str.push_back(s[u]);\n for(int v:adj[u]){\n if(visited[v]==false){\n dfs(v,adj,visited,s,index,str);\n }\n }\n }\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.length();\n vector<int>adj[n];\n for(int i=0;i<pairs.size();i++){\n adj[pairs[i][0]].push_back(pairs[i][1]);\n adj[pairs[i][1]].push_back(pairs[i][0]);\n }\n vector<bool>visited(n,false);\n string ans = s;\n for(int i=0;i<n;i++){\n if(visited[i]==false){\n\n vector<int>index;\n string str;\n dfs(i,adj,visited,s,index,str);\n\n int p=0;\n sort(index.begin(),index.end());\n sort(str.begin(),str.end());\n for(int j:index){\n ans[j] = str[p++];\n }\n }\n }\n return ans;\n\n }\n};",
"memory": "71037"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void findans(vector<vector<int>> &graph, vector<int> &visited, vector<int> &index, vector<char> &characters, string &s, int i){\n visited[i] = 1;\n index.push_back(i);\n characters.push_back(s[i]);\n for(int j=0; j<graph[i].size(); j++){\n if(visited[graph[i][j]]==0){\n findans(graph, visited, index, characters, s, graph[i][j]);\n }\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n vector<vector<int>> graph(n);\n vector<int> visited(n, 0);\n for(int i=0; i<pairs.size(); i++){\n graph[pairs[i][0]].push_back(pairs[i][1]);\n graph[pairs[i][1]].push_back(pairs[i][0]);\n }\n string ans = s;\n for(int i=0; i<n; i++){\n if(visited[i]==0){\n vector<int> index;\n vector<char> characters;\n findans(graph, visited, index, characters, s, i);\n sort(index.begin(), index.end());\n sort(characters.begin(), characters.end());\n for(int j=0; j<index.size(); j++){\n ans[index[j]] = characters[j];\n }\n }\n }\n return ans;\n }\n\n};",
"memory": "71562"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void dfs(vector<vector<int>> &g, vector<bool> &vis, vector<int> &index, string &str, int i, string &s) {\n vis[i] = true;\n str += s[i];\n index.push_back(i);\n\n for(int j=0;j<g[i].size();j++) {\n if(!vis[g[i][j]]) {\n dfs(g,vis,index,str,g[i][j],s);\n }\n }\n }\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n\n int l = s.length();\n vector<vector<int>> g(l);\n for(int i=0;i<pairs.size();i++) {\n g[pairs[i][0]].push_back(pairs[i][1]);\n g[pairs[i][1]].push_back(pairs[i][0]);\n }\n\n vector<bool> vis(l);\n for(int i=0;i<l;i++) {\n if(!vis[i]) {\n vector<int> index;\n string str = \"\";\n dfs(g, vis, index, str, i, s);\n sort(str.begin(), str.end());\n sort(index.begin(),index.end());\n for(int i=0;i<index.size();i++) {\n s[index[i]] = str[i];\n }\n }\n }\n return s;\n \n }\n};",
"memory": "71562"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int n;\n vector<int>a[100005];\n vector<int>res;\n vector<int>index;\n int vis[100001];\n string x;\n void dfs(int v){\n vis[v]=1;\n res.push_back(x[v]);\n index.push_back(v);\n for(auto u:a[v])\n {\n if(!vis[u])\n dfs(u);\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n n=s.size();\n string ans=s;\n x=s;\n for(auto u:pairs)\n {\n a[u[0]].push_back(u[1]);\n a[u[1]].push_back(u[0]);\n }\n for(int i=0;i<n;i++)\n {\n if(!vis[i]){\n res.clear();\n index.clear();\n dfs(i);\n sort(res.begin(),res.end());\n sort(index.begin(),index.end());\n int runner=0,ct=0;\n while(runner!=index.size())\n {\n ans[index[runner++]]=res[ct++];\n }\n }\n }\n return ans;\n\n\n }\n};",
"memory": "72087"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n void dfs(int idx, vector<int>&nodes ,vector<int>&visited, vector<vector<int>>&adj){\n nodes.push_back(idx);\n visited[idx] = 1;\n for(int it:adj[idx]){\n if(!visited[it]){\n dfs(it, nodes, visited, adj);\n }\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n vector<vector<int>>adj(s.size());\n for(int i=0;i<pairs.size();i++){\n adj[pairs[i][0]].push_back(pairs[i][1]);\n adj[pairs[i][1]].push_back(pairs[i][0]);\n }\n\n vector<int>visited(s.size(), 0);\n\n vector<vector<int>>components;\n for(int i=0;i<s.size();i++){\n vector<int>v;\n if(!visited[i]){\n dfs(i, v, visited, adj);\n\n }\n if(!v.empty())components.push_back(v);\n }\n vector<vector<char>>chars(components.size());\n for(int i=0;i<components.size();i++){\n for(int j = 0;j<components[i].size();j++){\n chars[i].push_back(s[components[i][j]]);\n }\n \n \n sort(chars[i].begin(), chars[i].end());\n sort(components[i].begin(), components[i].end());\n for(int k=0;k<components[i].size();k++){\n s[components[i][k]] = chars[i][k];\n }\n\n }\n return s;\n\n\n }\n};",
"memory": "72087"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<char> v;\n vector<int> u;\n void dfs(string &s,vector<vector<int>> &graph,int i,vector<int> &vis){\n if(vis[i]){\n return;\n }\n vis[i]=1;\n v.push_back(s[i]);\n u.push_back(i);\n // cout<<s[i]<<\" \"<<i<<endl;\n for(auto j:graph[i]){\n dfs(s,graph,j,vis);\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n // vector<vector<int>> v;\n int n=s.size();\n vector<int> vis(n,0);\n vector<vector<int>> graph(n);\n for(auto i:pairs){\n graph[i[0]].push_back(i[1]);\n graph[i[1]].push_back(i[0]);\n }\n // string out=s;\n for(int i=0;i<n;i++){\n if(!vis[i]){\n v.clear();\n u.clear();\n dfs(s,graph,i,vis);\n }\n sort(v.begin(),v.end());\n sort(u.begin(),u.end());\n for(int j=0;j<u.size();j++){\n // char c=v[j];\n // cout<<v[j]<<endl;\n s[u[j]]=v[j];\n }\n }\n return s;\n\n }\n};",
"memory": "72612"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int>adj[100001];\n int vis[100001];\n void dfs(int node,vector<int>&temp){\n vis[node]=1;\n temp.push_back(node);\n for(auto adjNode:adj[node]){\n if(!vis[adjNode])dfs(adjNode,temp);\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n for(auto it:pairs){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n for(int i=0;i<s.length();i++){\n if(!vis[i]){\n vector<int>temp;\n dfs(i,temp);\n vector<char>v;\n for(int i=0;i<temp.size();i++){\n v.push_back(s[temp[i]]);\n }\n sort(temp.begin(),temp.end());\n sort(v.begin(),v.end());\n for(int i=0;i<temp.size();i++){\n s[temp[i]]=v[i];\n }\n }\n }\n return s;\n }\n};",
"memory": "72612"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void dfs(vector<vector<int> > &graph, vector<bool> &visited, vector<int> &members, int i){\n visited[i] = true;\n members.push_back(i);\n\n for( auto nbr : graph[i] ){\n if( !visited[nbr] ){\n dfs(graph, visited, members, nbr);\n }\n }\n\n return;\n }\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n vector<vector<int> > graph(n);\n\n for( int i = 0; i < pairs.size(); i++){\n graph[pairs[i][1]].push_back(pairs[i][0]);\n graph[pairs[i][0]].push_back(pairs[i][1]);\n }\n\n vector<vector<int> > connectedIndices; \n vector<bool> visited(n, false);\n for( int i = 0; i < n; i++ ){\n if( visited[i] == false){\n vector<int> members;\n dfs(graph, visited, members, i);\n connectedIndices.push_back(members);\n }\n }\n\n for( int i = 0; i < connectedIndices.size(); i++){\n string temp = \"\";\n for( int j = 0; j < connectedIndices[i].size(); j++){\n temp+=s[connectedIndices[i][j]];\n }\n \n sort(temp.begin(), temp.end());\n sort(connectedIndices[i].begin(), connectedIndices[i].end());\n\n for( int j = 0; j < connectedIndices[i].size(); j++){\n s[connectedIndices[i][j]] = temp[j];\n }\n }\n\n return s;\n\n }\n};",
"memory": "73137"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void dfs(vector<vector<int> > &graph, vector<bool> &visited, vector<int> &members, int i){\n visited[i] = true;\n members.push_back(i);\n\n for( auto nbr : graph[i] ){\n if( !visited[nbr] ){\n dfs(graph, visited, members, nbr);\n }\n }\n\n return;\n }\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n vector<vector<int> > graph(n);\n\n for( int i = 0; i < pairs.size(); i++){\n graph[pairs[i][1]].push_back(pairs[i][0]);\n graph[pairs[i][0]].push_back(pairs[i][1]);\n }\n\n vector<vector<int> > connectedIndices; \n vector<bool> visited(n, false);\n for( int i = 0; i < n; i++ ){\n if( visited[i] == false){\n //cout<<\"eneter\"<<\" \";\n vector<int> members;\n dfs(graph, visited, members, i);\n connectedIndices.push_back(members);\n }\n }\n cout<<connectedIndices.size()<<\" \";\n for( int i = 0; i < connectedIndices.size(); i++){\n string temp = \"\";\n for( int j = 0; j < connectedIndices[i].size(); j++){\n cout<<connectedIndices[i][j]<<\" \";\n temp+=s[connectedIndices[i][j]];\n }\n //cout<<temp<<\" \";\n sort(temp.begin(), temp.end());\n sort(connectedIndices[i].begin(), connectedIndices[i].end());\n //cout<<temp<<\" \";\n\n for( int j = 0; j < connectedIndices[i].size(); j++){\n s[connectedIndices[i][j]] = temp[j];\n }\n }\n\n return s;\n\n }\n};",
"memory": "73137"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "struct UnionFind {\n vector<int> parents;\n \n UnionFind(size_t n): parents(n) {\n iota(parents.begin(), parents.end(), 0);\n }\n \n int find(int i) {\n while (i != parents[i]) {\n i = parents[i] = parents[parents[i]];\n }\n return i;\n }\n \n void do_union(const int i, const int j) {\n const int parent_i = find(i);\n const int parent_j = find(j);\n \n parents[parent_j] = parents[j] = parents[i];\n }\n \n};\n\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = size(s);\n UnionFind uf(n);\n \n for (auto &p : pairs) {\n uf.do_union(p[0], p[1]);\n }\n \n /* \n * countTable represent 2d of array \n * the first dimension is for parent of the union find node if the slot idx is not parent then we don't use it\n * the second dimension is represent number of character depend on the index. 0 = a, 1 = b, ..., 25 = z\n *\n * cursors represent current character of the countTable we from 0..=25, or we could say 'a' to 'z'\n */\n vector<vector<int>> countTable(n, vector<int>(26));\n vector<int> cursors(n);\n for (int i = 0; i < n; i++) {\n countTable[uf.find(i)][s[i] - 'a']++;\n }\n \n for (int i = 0; i < n; i++) {\n int j = uf.find(i);\n \n // move cursor until we find next character in the bucket\n while (countTable[j][cursors[j]] == 0) cursors[j]++;\n countTable[j][cursors[j]]--;\n \n // reuse input as a result\n s[i] = cursors[j] + 'a';\n }\n return s;\n }\n};",
"memory": "73662"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void dfs(int i,vector <int> &vis,string &s,vector <vector <int>> &adj,vector <int> &indexes, vector <char> &chars){\n vis[i]=true;\n chars.push_back(s[i]);\n indexes.push_back(i);\n for(auto &val:adj[i]){\n if(!vis[val]) dfs(val,vis,s,adj,indexes,chars);\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n //solution 2 - DFS\n vector <vector <int>> adj(s.size());\n for(auto &pair:pairs){\n adj[pair[0]].push_back(pair[1]);\n adj[pair[1]].push_back(pair[0]);\n }\n vector <int> vis(s.size());\n for(int i=0;i<s.size();i++){\n if(!vis[i]){\n vector <int> indexes;\n vector <char> chars;\n dfs(i,vis,s,adj,indexes, chars);\n //dfs complete, operate here only\n sort(indexes.rbegin(),indexes.rend());\n sort(chars.rbegin(),chars.rend());\n while(indexes.size()){\n s[indexes[indexes.size()-1]]=chars[indexes.size()-1];\n chars.pop_back();\n indexes.pop_back();\n }\n }\n }\n return s;\n }\n};",
"memory": "73662"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void solve(vector<vector<int>>& adj ,int i, vector<int> &vis , vector<int>& cap){\n if(vis[i] != 0){\n return;\n }\n cap.push_back(i);\n vis[i] =1;\n for(auto it : adj[i]){\n if(vis[it] == 0){\n solve(adj, it , vis , cap);\n }\n }\n }\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n vector<vector<int>> adj(s.length());\n for(auto it : pairs){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<int> vis(s.length() ,0);\n for(int i =0; i< s.length() ; i++){\n if(!vis[i]){\n vector<int> arr;\n solve(adj , i , vis , arr);\n vector<char> m;\n for(auto it : arr){\n m.push_back(s[it]);\n }\n sort(m.begin(), m.end());\n sort(arr.begin(), arr.end());\n for(int i = 0; i< arr.size(); i++){\n s[arr[i]]= m[i];\n }\n\n\n }\n }\n return s;\n }\n};",
"memory": "74187"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void DFS(vector<vector<int>>&Graph,int u,vector<int>&Vis,vector<int>&nodes){\n nodes.push_back(u);\n Vis[u]=1;\n for(int v:Graph[u]){\n if(!Vis[v]){\n DFS(Graph,v,Vis,nodes);\n }\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n vector<vector<int>>Graph(s.size(),vector<int>());\n vector<int>Vis(s.size(),0);\n for(auto Pair:pairs){\n Graph[Pair[0]].push_back(Pair[1]);\n Graph[Pair[1]].push_back(Pair[0]);\n }\n for(int i=0;i<s.size();i++){\n vector<int>nodes;\n if(!Vis[i]){\n DFS(Graph,i,Vis,nodes);\n vector<char>Letter;\n for(int j=0;j<nodes.size();j++){\n Letter.push_back(s[nodes[j]]);\n }\n sort(nodes.begin(),nodes.end());\n sort(Letter.begin(),Letter.end());\n for(int j=0;j<nodes.size();j++){\n s[nodes[j]]=Letter[j];\n }\n }\n }\n return s;\n }\n};",
"memory": "74187"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(vector<int> &ind, vector<char> &letter,vector<int> adj[], int i, vector<int> &vis, string &s){\n vis[i] = 1;\n ind.push_back(i);\n letter.push_back(s[i]);\n\n for(auto it: adj[i])\n {\n if(!vis[it]){\n dfs(ind,letter,adj,it,vis,s);\n }\n }\n }\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& arr) {\n int n = s.size();\n vector<int> adj[n];\n vector<int> vis(n,0);\n for(auto it: arr){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n\n for(int i = 0;i<arr.size();i++){\n vector<int> ind;\n vector<char> letter ;\n\n if(!vis[i]){\n dfs(ind,letter,adj,i,vis,s);\n }\n\n sort(ind.begin(),ind.end());\n sort(letter.begin(),letter.end());\n\n for(int i = 0;i<ind.size();i++){\n s[ind[i]] = letter[i];\n }\n }\n\n return s;\n }\n};",
"memory": "74712"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "\nclass Solution {\npublic:\n typedef vector<vector<int>> Graph ;\n void add_undirected_edge (Graph & graph,int from , int to)\n {\n graph[from].push_back(to);\n graph[to].push_back(from);\n }\n void dfs (Graph& graph , int node , vector<bool>&visited,vector<int>& c_index)\n {\n visited[node] = 1 ;\n c_index.push_back(node);\n for (auto neighbour : graph[node])\n if (!visited[neighbour])dfs(graph,neighbour,visited,c_index);\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int nodes = s.size();\n // build graph \n Graph graph (nodes);\n for(auto pairr : pairs)\n add_undirected_edge (graph,pairr[0],pairr[1]);\n //--------\n vector<bool> visited (nodes,0);\n for (int i = 0 ; i < nodes;i++)\n {\n if (!visited[i]&& graph[i].size()>0)\n {\n vector<int> c_index ;\n dfs(graph,i,visited,c_index);\n vector<char> c_char ;\n for (auto index : c_index)\n c_char.push_back(s[index]);\n sort(c_index.begin(),c_index.end());\n sort(c_char.begin(),c_char.end());\n int j = 0 ;\n for (auto index : c_index)\n s[index] = c_char[j] , j++ ;\n }\n }\n return s ;\n }\n};",
"memory": "74712"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\nvoid dfs(int node,vector<char>&chars,vector<int>&indices,vector<int>adjList[],vector<int>& vis,string &s){\n vis[node]=1;\n chars.push_back(s[node]);\n indices.push_back(node);\n\n for(auto it:adjList[node]){\n if(!vis[it]){\n dfs(it,chars,indices,adjList,vis,s);\n }\n }\n}\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.size();\n vector<int>adjList[n];\n\n for(auto it:pairs){\n int u=it[0];\n int v=it[1];\n adjList[u].push_back(v);\n adjList[v].push_back(u);\n }\n\n vector<int>vis(n);\n string ans=\"\";\n for(int i=0;i<n;i++)ans+=\" \";\n\n for(int i=0;i<n;i++){\n if(!vis[i]){\n vector<int>indices;\n vector<char>chars;\n dfs(i,chars,indices,adjList,vis,s);\n sort(indices.begin(),indices.end());\n sort(chars.begin(),chars.end());\n\n for(int i=0;i<indices.size();i++)ans[indices[i]]=chars[i];\n }\n }\n\n return ans;\n }\n};",
"memory": "75237"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Comp {\nprivate:\n vector<char> chars;\n int ptr;\npublic:\n Comp(vector<char>& idx) {\n chars = idx;\n ptr = 0;\n build();\n }\n\n Comp() {\n ptr = 0;\n }\n\n char curr() {\n return chars[ptr++];\n } \n\n void add(char c) {\n chars.push_back(c);\n } \n\n void build() {\n sort(chars.begin(), chars.end());\n }\n};\n\n\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n vector<Comp*> comps(s.length());\n\n vector<vector<int>> adj(s.length());\n\n for (auto pair: pairs) {\n adj[pair[0]].push_back(pair[1]);\n adj[pair[1]].push_back(pair[0]);\n }\n\n vector<int> vis(s.length(), 0);\n\n\n for (int i = 0; i < s.length(); i++) {\n if (vis[i] == 0) {\n Comp* comp = new Comp();\n comps[i] = comp;\n fun(i, s, adj, vis, comps);\n comps[i]->build();\n }\n }\n\n string op = \"\";\n\n\n for (int i = 0; i < s.length(); i++) {\n op.push_back(comps[i]->curr());\n }\n\n return op;\n }\n\n void fun(int i, string& s, vector<vector<int>>& adj, vector<int>& vis, vector<Comp*>& comps) {\n vis[i] = 1;\n comps[i]->add(s[i]);\n\n for (auto child: adj[i]) {\n if (vis[child] == 0) {\n comps[child] = comps[i];\n fun(child, s, adj, vis, comps);\n }\n }\n }\n};",
"memory": "75237"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "\n\n\nclass Solution {\n\nprivate:\n\n void dfs(int i, string& s, vector<vector<int>>& adj, int color, vector<bool>& visited, auto& m1, auto& m2){\n if(visited[i]) return;\n visited[i] = true;\n m1[color].push(s[i]);\n m2[color].push(i);\n\n for(int u : adj[i]){\n dfs(u, s, adj, color, visited, m1, m2);\n }\n }\n\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.length();\n vector<bool> visited(n, false);\n vector<vector<int>> adj(n);\n\n for(vector<int>& pair : pairs){\n int a = pair[0];\n int b = pair[1];\n adj[a].push_back(b);\n adj[b].push_back(a);\n }\n\n int cc = 1;\n unordered_map<int, priority_queue<char, vector<char>, greater<char>>> m1;\n unordered_map<int, priority_queue<int, vector<int>, greater<int>>> m2;\n\n for(int i = 0 ; i < n; i++){\n dfs(i, s, adj, cc, visited, m1, m2);\n cc++;\n }\n\n for(auto& e : m1){\n auto &p1 = e.second;\n auto &p2 = m2[e.first];\n\n while(!p1.empty()) {\n char c = p1.top();\n int idx = p2.top();\n s[idx] = c;\n p1.pop();\n p2.pop();\n }\n }\n\n\n return s;\n }\n};",
"memory": "75762"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n void dfs(vector<vector<int>>& adj, int i, set<int>& us) {\n // cout<<i<<\" \";\n if (us.find(i) != us.end())return ;\n cout<<i<<\" \";\n us.insert(i);\n for(auto it : adj[i]) \n dfs(adj, it, us);\n }\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.length();\n vector<vector<int>> adj(n);\n for(int i=0;i<pairs.size();i++) {\n adj[pairs[i][0]].push_back(pairs[i][1]);\n adj[pairs[i][1]].push_back(pairs[i][0]);\n }\n // cout<<adj[0][0]<<\" \";\n vector<bool> visited(n,0);\n for(int i=0;i<n;i++) {\n if (!visited[i]) {\n set<int> us;\n dfs(adj, i, us);\n vector<char> x;\n for(auto it : us) {\n visited[it] = true;\n x.push_back(s[it]);\n }\n sort(x.begin(), x.end());\n int j=0;\n for(auto it: us) {\n //cout<<it<<\" \";\n s[it] = x[j];\n j++;\n }\n \n }\n }\n return s;\n }\n};",
"memory": "75762"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.size();\n vector<vector<int>> a(n);\n\n for(auto i:pairs)\n {\n a[i[0]].push_back(i[1]);\n a[i[1]].push_back(i[0]);\n }\n\n for(int i=0;i<n;i++)\n {\n cout<<i<<\"->\";\n for(auto j:a[i])\n {\n cout<<j<<\" \";\n }\n cout<<endl;\n }\n vector<int> vis(n,0);\n for(int i=0;i<n;i++)\n {\n if(vis[i]==1||a[i].size()==0) continue;\n stack<int> st;\n string s1=\"\";\n // s1+=s[i];\n // cout<<s1<<\" \"<<i<<endl;\n set<int> index;\n st.push(i);\n while(!st.empty())\n {\n int i1=st.top();\n // cout<<i1<<endl;\n st.pop();\n if(vis[i1]==1) continue;\n s1+=s[i1];\n index.insert(i1);\n vis[i1]=1;\n for(auto j:a[i1])\n {\n st.push(j);\n }\n \n }\n sort(s1.begin(),s1.end());\n cout<<s1<<endl;\n int i1=0;\n for(auto j:index)\n {\n s[j]=s1[i1++];\n }\n }\n return s;\n }\n};",
"memory": "76287"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(int node, vector<int>&path, string &temp_string, vector<bool> &vis, string &s, vector<vector<int>>&g){\n vis[node] = true;\n path.push_back(node);\n temp_string.push_back(s[node]);\n\n for(int v: g[node]){\n if(vis[v]) continue;\n else dfs(v, path, temp_string, vis, s, g);\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int N = s.size();\n vector<vector<int>>g(N);\n vector<bool>vis(N, false);\n for(vector<int> e : pairs){\n g[e[0]].push_back(e[1]);\n g[e[1]].push_back(e[0]);\n }\n\n for(int i = 0; i < N; i++){\n if(!vis[i]){\n vector<int> path;\n string temp = \"\";\n dfs(i, path, temp, vis, s, g);\n sort(path.begin(), path.end());\n sort(temp.begin(), temp.end());\n\n for(int j = 0; j < path.size(); j++) s[path[j]] = temp[j];\n }\n }\n\n return s;\n }\n};",
"memory": "76287"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n void visit(string& s, vector<vector<int>>& adjList, vector<bool>& vis, int ind, string& currAns, vector<int>& currInd)\n {\n vis[ind] = true;\n currAns.push_back(s[ind]);\n currInd.push_back(ind);\n\n for(int neigh: adjList[ind])\n {\n if(!vis[neigh])\n {\n visit(s, adjList, vis, neigh, currAns, currInd);\n }\n }\n }\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.length();\n vector<vector<int>> adjList(n);\n for(auto pair: pairs)\n {\n adjList[pair[0]].push_back(pair[1]);\n adjList[pair[1]].push_back(pair[0]);\n }\n\n string ans(n, ' ');\n vector<bool> vis(n, false);\n for(int i=0;i<n;i++)\n {\n if(!vis[i])\n {\n string currAns = \"\";\n vector<int> currInd;\n visit(s, adjList, vis, i, currAns, currInd);\n\n sort(currAns.begin(), currAns.end());\n sort(currInd.begin(), currInd.end());\n for(int i=0;i<currAns.length();i++)\n {\n ans[currInd[i]]=currAns[i];\n }\n }\n }\n return ans;\n }\n};",
"memory": "76812"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n\n void dfs(string &s, int node,vector<int> &ind, vector<bool> &vis, vector<vector<int>> &adj, string &x)\n {\n vis[node]=true;\n ind.push_back(node);\n x+=s[node];\n for(auto it: adj[node])\n if(!vis[it])\n dfs(s,it,ind,vis,adj,x);\n\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int k=s.size();\n vector<vector<int>> adj(k);\n vector<bool> vis(k, false);\n string ans = s;\n for(auto it:pairs)\n {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n\n }\n for(int i=0;i<k;i++)\n {\n if(!vis[i])\n {\n vector<int> ind;\n string x=\"\";\n dfs(s,i,ind,vis,adj,x);\n sort(x.begin(),x.end());\n sort(ind.begin(),ind.end());\n for(int i=0;i<ind.size();i++)\n ans[ind[i]]=x[i];\n\n }\n }\n return ans;\n }\n};",
"memory": "76812"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(int pos, vector<int>adj[], vector<int>&vis,vector<int>&group, int top){\n vis[pos]=1;\n group[pos]=top;\n for(auto it: adj[pos]){\n if(vis[it]==0)dfs(it,adj,vis,group,top);\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n if(pairs.size()==0)return s;\n int n= s.size();\n vector<int>adj[n];\n for(auto &it:pairs){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<int>vis(n,0);\n vector<int>group(n);\n for(int i=0;i<n;i++){\n group[i]=i;\n }\n for(int i=0;i<n;i++){\n if(vis[i]==0)dfs(i,adj,vis,group,i);\n }\n for(int i=0;i<n;i++){\n cout<<group[i]<<\" \";\n }\n cout<<endl;\n // return \"Yes\";\n unordered_map<int,vector<int>>mp;\n for(int i=0;i<n;i++){\n mp[group[i]].push_back(i);\n }\n // int maxi=INT_MIN;\n // for(int i=0;i<mp.size();i++){\n // int size=mp[i].size();\n // maxi= max(maxi,size);\n // }\n for(int i=0;i<mp.size();i++){\n // if(mp[i].size()==maxi){\n string temp=\"\";\n for(int j=0;j<mp[i].size();j++){\n temp+=s[mp[i][j]];\n }\n sort(temp.begin(),temp.end());\n for(int j=0;j<mp[i].size();j++){\n s[mp[i][j]]=temp[j];\n }\n\n // }\n }\n return s;\n }\n};",
"memory": "77337"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n vector<vector<int> > adj(s.size());\n for(int i=0;i<pairs.size();i++){\n adj[pairs[i][0]].push_back(pairs[i][1]);\n adj[pairs[i][1]].push_back(pairs[i][0]);\n } \n vector<bool> vis(s.size(),false);\n for(int i=0;i<s.size();i++){\n if(vis[i]==false){\n vector<int> ind;\n string cur;\n queue<int> qu;\n qu.push(i);\n vis[i]=true;\n while(!qu.empty()){\n ind.push_back(qu.front());\n cur.push_back(s[qu.front()]);\n qu.pop();\n for(int j=0;j<adj[ind[ind.size()-1]].size();j++){\n if(vis[adj[ind[ind.size()-1]][j]]==false){\n vis[adj[ind[ind.size()-1]][j]] = true;\n qu.push(adj[ind[ind.size()-1]][j]);\n }\n }\n }\n sort(cur.begin(),cur.end());\n sort(ind.begin(),ind.end());\n for(int j=0;j<ind.size();j++){\n s[ind[j]]=cur[j];\n }\n }\n }\n return s;\n }\n};",
"memory": "77337"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n vector<vector<int>> graph(n);\n unordered_set<int> visited;\n vector<int> visited_idx;\n string cc;\n\n for (auto& pair : pairs) {\n graph[pair[0]].push_back(pair[1]);\n graph[pair[1]].push_back(pair[0]);\n }\n\n for (int i = 0; i < n; ++i) {\n if (visited.count(i) == 0) {\n cc.clear();\n visited_idx.clear();\n dfs(s, graph, cc, visited, visited_idx, i);\n sort(cc.begin(), cc.end());\n sort(visited_idx.begin(), visited_idx.end());\n for (int k = 0; k < cc.size(); ++k) {\n s[visited_idx[k]] = cc[k];\n }\n }\n }\n\n return s;\n }\nprivate:\n void dfs(string &s, vector<vector<int>> &graph, string &cc, unordered_set<int> &visited, vector<int> &visited_idx, int u) {\n visited.insert(u);\n visited_idx.push_back(u);\n cc += s[u];\n \n for (auto &v : graph[u]) {\n if (visited.count(v) == 0) {\n dfs(s, graph, cc, visited, visited_idx, v);\n }\n }\n\n return;\n }\n};",
"memory": "77862"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(string &s,vector<vector<int>> &adj,vector<bool> &vis,int sv,priority_queue<int, vector<int>, greater<int>> &pq1,priority_queue<char, vector<char>, greater<char>> &pq2 ){\n vis[sv]=true;\n pq1.push(sv);\n pq2.push(s[sv]);\n for(auto it:adj[sv]){\n if(!vis[it]){\n dfs(s,adj,vis,it,pq1,pq2);\n }\n }\n }\n\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.length();\n vector<vector<int>> adj(n);\n for (auto it : pairs) {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n string ans(s);\n vector<bool> vis(n, false);\n for (int i = 0; i < n; i++) {\n if (!vis[i]) {\n priority_queue<char, vector<char>, greater<char>> pq2;\n priority_queue<int, vector<int>, greater<int>> pq1;\n dfs(s,adj,vis,i,pq1,pq2);\n while(!pq1.empty()){\n ans[pq1.top()]=pq2.top();\n pq1.pop();\n pq2.pop();\n }\n }\n }\n return ans;\n }\n};",
"memory": "77862"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "#include <vector>\n#include <string>\n#include <queue>\n#include <algorithm>\n\nusing namespace std;\n\nclass Solution {\npublic:\n void bfs(int start, vector<vector<int>>& adj, vector<bool>& visited, vector<int>& component) {\n queue<int> q;\n q.push(start);\n visited[start] = true;\n\n while (!q.empty()) {\n int node = q.front();\n q.pop();\n component.push_back(node);\n\n for (int neighbor : adj[node]) {\n if (!visited[neighbor]) {\n visited[neighbor] = true;\n q.push(neighbor);\n }\n }\n }\n }\n \n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.length();\n vector<vector<int>> adj(n);\n \n // Xây dựng đồ thị với các cạnh từ cặp hoán đổi\n for (const auto& pair : pairs) {\n adj[pair[0]].push_back(pair[1]);\n adj[pair[1]].push_back(pair[0]);\n }\n \n vector<bool> visited(n, false);\n \n // Xử lý từng thành phần liên thông trong đồ thị\n for (int i = 0; i < n; ++i) {\n if (!visited[i]) {\n vector<int> component;\n bfs(i, adj, visited, component);\n \n // Sắp xếp các chỉ số và ký tự tương ứng\n string chars;\n for (int index : component) {\n chars += s[index];\n }\n sort(component.begin(), component.end());\n sort(chars.begin(), chars.end());\n \n // Đặt lại các ký tự vào chuỗi ban đầu theo thứ tự nhỏ nhất\n for (int j = 0; j < component.size(); ++j) {\n s[component[j]] = chars[j];\n }\n }\n }\n \n return s;\n }\n};\n",
"memory": "78387"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\nvoid fun(int i,vector<vector<int>>&v,vector<bool>&vis,vector<int>&res){\n if(vis[i])return ;\n \n vis[i]=1;\n \n res.push_back(i);\n \n for(int k=0;k<v[i].size();k++)\n fun(v[i][k],v,vis,res);\n\n}\n\nvoid fill(string &str,vector<int> &res,set<pair<int,char>>&s){\n \n string temp=\"\";\n for(int i=0;i<res.size();i++)\n temp.push_back(str[res[i]]);\n\n sort(temp.begin(),temp.end());\n sort(res.begin(),res.end());\n \n int c=0;\n for(int i=0;i<res.size();i++)\n s.insert({res[i],temp[i]});\n \n}\n\nstring smallestStringWithSwaps(string str, vector<vector<int>> &pairs){\n vector<vector<int>>v(str.size());\n vector<bool>vis(str.size(),0);\n // map<pair<char,int>,bool>st;\n // map<pair<char,int>,int>idx;\n set<pair<int,char>>s;\n for(int i=0;i<pairs.size();i++){\n v[pairs[i][0]].push_back(pairs[i][1]);\n v[pairs[i][1]].push_back(pairs[i][0]);\n }\n\n string ans=\"\";\n for(int i=0;i<str.size();i++){\n vector<int>res;\n fun(i,v,vis,res); \n fill(str,res,s);\n }\n for(auto i:s)\n ans+=i.second;\n \n return ans;\n}\n\n\n};",
"memory": "79962"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int n;\n vector<int> G[100005];\n string str;\n vector<int> chars;\n set<int> indices;\n void dfs(int node , vector<int>& vis) {\n // chars.insert(str[node]);\n chars[str[node]-'a']++;\n indices.insert(node);\n vis[node] = 1;\n // cout << node << endl;\n // int sz = G[node].size();\n // cout << node << \" \" << vis[node] << endl;\n for (int adj: G[node]) {\n if( !vis[adj] ) {\n dfs(adj, vis);\n }\n }\n return;\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n\n n = s.size();\n str = s;\n for (auto pair : pairs) {\n G[pair[0]].push_back(pair[1]);\n G[pair[1]].push_back(pair[0]);\n }\n // for \n vector<int> vis(n,0);\n string ans = s;\n vector<int> fre(26,0);\n chars = fre;\n \n for (int i = 0; i < n; i++) {\n\n if (!vis[i]) {\n // chars = \n indices = {};\n // cout << chars.size() << endl;\n dfs(i, vis);\n // cout << chars.size() << endl;\n auto it1 = indices.begin();\n // auto it = chars.begin();\n // while (it != chars.end() && it1 != indices.end()) {\n // int idx = *it1;\n // char c = *it;\n // // cout << idx << \" \" << c << endl;\n // ans[idx] = c;\n // it++;\n // it1++;\n // }\n for (int i = 0; i < 26; i++) {\n if (chars[i] > 0) {\n while (chars[i] > 0) {\n int idx = *it1;\n char c = 'a' + i;\n ans[idx] = c;\n it1++;\n chars[i]-=1;\n }\n }\n }\n }\n }\n\n return ans;\n \n }\n};",
"memory": "79962"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n\n int n = s.size();\n vector<vector<int>> graph(n);\n\n for (auto v : pairs){ // constructed a graph\n\n graph[v[0]].push_back(v[1]);\n graph[v[1]].push_back(v[0]);\n }\n\n vector<vector<int>> indices;\n vector<string> values;\n vector<bool> visited(n,false);\n\n for (int i=0; i<n; i++){\n if (!visited[i]){\n vector<int> temp;\n string res;\n dfs(graph,i,visited,temp,res,s);\n indices.push_back(temp);\n values.push_back(res);\n }\n }\n\n int v_size = indices.size();\n\n for (int i=0;i<v_size; i++){\n \n sort(indices[i].begin(),indices[i].end());\n sort(values[i].begin(),values[i].end());\n\n int s_size = values[i].size();\n\n for (int j=0; j<s_size; j++){\n s[indices[i][j]] = values[i][j];\n }\n }\n\n return s;\n }\n//dfs(graph,i,visited,temp,res,s);\n void dfs(vector<vector<int>> &graph, int i, vector<bool> &visited, vector<int>& temp, string& res, string &s){\n\n visited[i] = true;\n temp.push_back(i);\n res += s[i];\n\n for (int val : graph[i]){\n if (!visited[val]){\n\n dfs(graph,val, visited, temp,res,s);\n }\n }\n }\n};",
"memory": "80487"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int>parent;\n vector<int>rank;\n int find(int u){\n if(parent[u]==u)return u;\n return parent[u]=find(parent[u]);\n }\n void dsu(int u,int v){\n int x=find(u);\n int y=find(v);\n if(x==y)return;\n if(rank[x]>rank[y]){\n parent[y]=x;\n }\n else if(parent[y]>parent[x]){\n parent[x]=y;\n }\n else {\n parent[y]=x;\n rank[x]++;\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.size();\n parent.resize(n,0);\n for(int i=0;i<n;i++){\n parent[i]=i;\n }\n rank.resize(n,0);\n for(auto it:pairs){\n int u=it[0];\n int v=it[1];\n dsu(u,v);\n }\n unordered_map<int,set<int>>m;\n for(auto it:pairs){\n int x=find(it[0]);\n int v=find(it[1]);\n m[x].insert(it[0]);\n m[v].insert(it[1]);\n }\n for(auto it:m){\n vector<int>check;\n vector<char>let;\n for(auto &v:it.second){\n check.push_back(v);\n let.push_back(s[v]);\n }\n sort(check.begin(),check.end());\n sort(let.begin(),let.end());\n for(int i=0;i<check.size();i++){\n s[check[i]]=let[i];\n }\n }\n return s;\n }\n};",
"memory": "80487"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n typedef unordered_map<int, vector<int>> GRAPH;\n GRAPH graph;\n vector<bool> visited;\n\n void addEdge(int u, int v) {\n graph[u].push_back(v);\n graph[v].push_back(u);\n }\n\n void dfs(int node, vector<int>& indices, vector<char>& chars, string& s) {\n visited[node] = true;\n indices.push_back(node);\n chars.push_back(s[node]);\n for (int neighbor : graph[node]) {\n if (!visited[neighbor]) {\n dfs(neighbor, indices, chars, s);\n }\n }\n }\n\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int length = s.length();\n visited.resize(length, false);\n\n // Build the graph\n for (const auto& pair : pairs) {\n addEdge(pair[0], pair[1]);\n }\n\n // Process each connected component\n for (int i = 0; i < length; ++i) {\n if (!visited[i]) {\n vector<int> indices;\n vector<char> chars;\n dfs(i, indices, chars, s);\n\n // Sort the characters and indices\n sort(indices.begin(), indices.end());\n sort(chars.begin(), chars.end());\n\n // Place sorted characters back into the string\n for (int j = 0; j < indices.size(); ++j) {\n s[indices[j]] = chars[j];\n }\n }\n }\n\n return s;\n }\n};\n\n",
"memory": "81012"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n\n vector<vector<int>> adj(n);\n\n for(auto x : pairs){\n adj[x[0]].push_back(x[1]);\n adj[x[1]].push_back(x[0]);\n }\n\n map<int, vector<char>> comp;\n map<int, vector<int>> idx;\n vector<bool> vis(n);\n\n int color = 0;\n\n function<void(int)> dfs = [&](int node){\n vis[node] = true;\n \n comp[color].push_back(s[node]);\n idx[color].push_back(node);\n\n for(int neighbor : adj[node]){\n if(!vis[neighbor]) dfs(neighbor);\n }\n };\n\n for(int i = 0; i < n; i++){\n if(!vis[i]) dfs(i), color++;\n }\n\n vector<char> ans(n);\n\n\n for(int i = 0; i < color; i++){\n sort(idx[i].begin(), idx[i].end());\n sort(comp[i].begin(), comp[i].end());\n for(int j = 0; j < comp[i].size(); j++){\n ans[idx[i][j]] = comp[i][j];\n }\n }\n\n string finalAns = \"\";\n\n for(char c : ans){\n finalAns += c;\n }\n\n return finalAns;\n\n } \n};",
"memory": "81012"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void find_connection(vector<vector<int>>&adj, set<int>&connected_nodes, int idx, vector<int>&vis){\n connected_nodes.insert(idx);\n vis[idx] = 1;\n for(auto& ele: adj[idx]){\n if(vis[ele]==0)\n find_connection(adj,connected_nodes,ele,vis);\n }\n }\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n\n int len = s.length();\n vector<vector<int>>adj(len);\n for(auto& ele: pairs){\n adj[ele[0]].push_back(ele[1]);\n adj[ele[1]].push_back(ele[0]);\n }\n vector<int>vis(len,0);\n vector<vector<int>> connection_matrix;\n for(int i=0; i<len; i++){\n if(vis[i]==0){\n set<int>connected_nodes;\n find_connection(adj,connected_nodes,i,vis);\n vector<int>vec;\n for(auto& ele: connected_nodes){\n vec.push_back(ele);\n }\n sort(vec.begin(),vec.end());\n connection_matrix.push_back(vec);\n }\n }\n for(auto& vec: connection_matrix){\n vector<char> str;\n for(auto&idx: vec){\n str.push_back(s[idx]);\n }\n sort(str.begin(),str.end());\n int i=0;\n for(auto&idx: vec){\n s[idx] = str[i];\n i++;\n }\n }\n return s;\n }\n};",
"memory": "81537"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "#include <vector>\n#include <string>\n#include <unordered_map>\n#include <unordered_set>\n#include <algorithm>\n#include <queue>\n\nusing namespace std;\n\nclass Solution {\npublic:\n void dfs(int node, unordered_map<int, vector<int>>& graph, vector<int>& visited, vector<int>& component) {\n visited[node] = 1;\n component.push_back(node);\n for (int neighbor : graph[node]) {\n if (!visited[neighbor]) {\n dfs(neighbor, graph, visited, component);\n }\n }\n }\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n unordered_map<int, vector<int>> graph;\n for (const vector<int>& pair : pairs) {\n graph[pair[0]].push_back(pair[1]);\n graph[pair[1]].push_back(pair[0]);\n }\n vector<int> visited(s.size(), 0);\n vector<vector<int>> components;\n for (int i = 0; i < s.size(); i++) {\n if (!visited[i]) {\n vector<int> component;\n dfs(i, graph, visited, component);\n components.push_back(component);\n }\n }\n string result = s;\n for (const vector<int>& component : components) {\n vector<int> indices = component;\n vector<char> chars;\n for (int index : indices) {\n chars.push_back(s[index]);\n }\n sort(indices.begin(), indices.end());\n sort(chars.begin(), chars.end());\n\n for (int i = 0; i < indices.size(); i++) {\n result[indices[i]] = chars[i];\n }\n }\n return result;\n }\n};\n",
"memory": "82062"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n \npublic:\n\nvoid dfs(int index, vector<set<int>> &graph, vector<bool> &visited, vector<int> &component) {\n cin.tie(nullptr); cout.tie(nullptr); ios_base::sync_with_stdio(false);\n visited[index] = true;\n component.push_back(index); \n\n \n for (int neighbor : graph[index]) {\n if (!visited[neighbor]) {\n dfs(neighbor, graph, visited, component);\n }\n }\n}\n\nstring smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n cin.tie(nullptr); cout.tie(nullptr); ios_base::sync_with_stdio(false);\n int n = s.size();\n vector<set<int>> graph(n);\n\n\n for (const auto& pair : pairs) {\n graph[pair[0]].insert(pair[1]);\n graph[pair[1]].insert(pair[0]);\n }\n \n vector<bool> visited(n, false);\n\n \n for (int i = 0; i < n; ++i) {\n if (!visited[i]) {\n vector<int> component;\n dfs(i, graph, visited, component);\n\n \n vector<char> chars;\n for (int i : component) chars.push_back(s[i]);\n \n \n \n sort(component.begin(), component.end()); \n sort(chars.begin(), chars.end()); \n \n \n for (int j = 0; j < component.size(); ++j) s[component[j]] = chars[j];\n \n \n }\n }\n\n return s;\n}\n};",
"memory": "82062"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "struct DSU {\n\tvector<int> p;\n\tvector<int> s;\n\t\n \t// constructor initilisation\n\tDSU(int size) : p(size), s(size, 1) {\n\t\tfor (int i = 0; i < size; i++) { p[i] = i; }\n\t}\n\n // finding the identifer of set containing element x\n\tint find(int x) {\n\t\treturn p[x] == x ? x : (p[x] = find(p[x]));\n\t}\n\t\n\t// union of two sets set containing x & set containing y\n\tbool unite(int x, int y) {\n\t\tx = find(x);\n\t\ty = find(y);\n\t\tif (x == y) return false;\n\t\tif(s[x] > s[y]) swap(x, y);\n\t\ts[y] += s[x];\n\t\ts[x] = s[y]; \n\t\tp[x] = y;\n\t\treturn true; \n\t}\n\t\n\t// return true if the element x and y belongs to same set else false\n\tbool sameSet(int x, int y) { return find(x) == find(y); }\n\t\n\t// returns size of set containing element x\n\tint sizeOfSet(int x){ return s[find(x)]; }\n};\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& a) {\n int n = s.size();\n DSU dsu(n);\n map<int, set<int>> m;\n for(int i = 0; i < (int)a.size(); i++){\n int x = a[i][0];\n int y = a[i][1];\n if(!dsu.sameSet(x, y)){\n dsu.unite(x, y);\n }\n }\n for(int i = 0; i < n; i++){\n int p = dsu.find(i);\n m[p].insert(i);\n }\n map<int, char> resm;\n for(auto v : m){\n string t = \"\";\n for(auto x : v.second){\n t += s[x];\n }\n sort(t.begin(), t.end());\n int i = 0;\n for(auto x : v.second){\n resm[x] = t[i];\n i++;\n }\n }\n string res = \"\";\n for(auto x : resm){\n res += x.second;\n }\n return res;\n }\n};",
"memory": "82587"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.size();\n unordered_map<int, vector<int>> graph;\n vector<bool> isVisit(n, false);\n string temp=\"\";\n vector<int> idx;\n for(auto vec:pairs){\n graph[vec[0]].push_back(vec[1]);\n graph[vec[1]].push_back(vec[0]);\n }\n \n // Lambda 函数用于 DFS\n function<void(int)> dfs = [&](int u){\n if (isVisit[u]) return;\n isVisit[u] = true;\n temp += s[u];\n idx.push_back(u);\n for (int v : graph[u]) \n dfs(v);\n };\n\n for(int i=0;i<n;i++){\n if(isVisit[i]) continue;\n temp.clear();\n idx.clear();\n dfs(i);\n sort(temp.begin(), temp.end());\n sort(idx.begin(), idx.end());\n for(int j=0;j<idx.size();j++){\n s[idx[j]] = temp[j];\n }\n }\n return s;\n }\n};",
"memory": "83112"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.size();\n unordered_map<int, vector<int>> graph;\n vector<bool> isVisit(n, false);\n string temp=\"\";\n vector<int> idx;\n for(auto vec:pairs){\n graph[vec[0]].push_back(vec[1]);\n graph[vec[1]].push_back(vec[0]);\n }\n\n // Lambda 函数用于 DFS\n function<void(int)> dfs = [&](int u){\n isVisit[u] = true;\n temp += s[u];\n idx.push_back(u);\n for (int v : graph[u]){\n if (isVisit[v]) continue;\n dfs(v);\n }\n };\n\n for(int i=0;i<n;i++){\n if(isVisit[i]) continue;\n temp.clear();\n idx.clear();\n dfs(i);\n sort(temp.begin(), temp.end());\n sort(idx.begin(), idx.end());\n for(int j=0;j<idx.size();j++){\n s[idx[j]] = temp[j];\n }\n }\n return s;\n }\n};",
"memory": "83112"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n unordered_map<int, vector<int>> adj;\n string str;\n\n void dfs(int node, vector<int>& vis, vector<int>& indices, vector<char>& chars) {\n vis[node] = 1;\n indices.push_back(node);\n chars.push_back(str[node]);\n\n for (auto it : adj[node]) {\n if (!vis[it]) {\n dfs(it, vis, indices, chars);\n }\n }\n }\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n adj.clear();\n str = s;\n int n = s.size();\n\n \n for (auto it : pairs) {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n\n vector<int> vis(n, 0);\n\n for (int i = 0; i < n; i++) {\n if (!vis[i]) {\n vector<int> indices;\n vector<char> chars;\n dfs(i, vis, indices, chars);\n sort(indices.begin(), indices.end());\n sort(chars.begin(), chars.end());\n \n for (int j = 0; j < indices.size(); j++) {\n s[indices[j]] = chars[j];\n }\n }\n }\n\n return s;\n }\n};",
"memory": "83637"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> bfs(int node, string&s, vector<vector<int>>&adj, vector<int>&vis){\n vector<int>res;\n queue<int> q;\n q.push(node);\n vis[node]=1;\n \n while(!q.empty()){\n int nd = q.front();\n res.push_back(nd);\n q.pop();\n for(auto it: adj[nd]){\n if(!vis[it]){\n vis[it]=1; \n q.push(it);\n }\n }\n }\n return res;\n\n\n }\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n vector<vector<int>> adj(n);\n for(auto it: pairs){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<int> vis(n,0);\n for(int i=0; i<n; i++){\n if(!vis[i]){\n vector<int>tp = bfs(i,s,adj,vis);\n string str = \"\";\n for(auto it: tp){str.push_back(s[it]);}\n sort(str.begin(),str.end());\n sort(tp.begin(),tp.end());\n for(int i=0; i<str.size();i++){s[tp[i]] = str[i];}\n }\n }\n return s;\n\n\n }\n};",
"memory": "83637"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int n;\n\n vector<vector<int>> adjs;\n\n void initGraph(int n)\n {\n adjs = vector<vector<int>>(n);\n }\n\n void addEdge(int u, int v)\n {\n adjs[u].push_back(v);\n adjs[v].push_back(u);\n }\n\n vector<bool> visited;\n\n vector<int> bfs(int start)\n {\n vector<int> components;\n std::queue<int> q;\n q.push(start);\n visited[start] = true;\n\n while(!q.empty())\n {\n auto u = q.front();\n q.pop();\n\n components.push_back(u);\n\n for(auto v: adjs[u] )\n {\n if(!visited[v])\n {\n visited[v] = true;\n q.push(v);\n }\n }\n }\n\n return components;\n }\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n n = s.size();\n\n visited = vector<bool> (n, false);\n\n\n initGraph(n);\n\n for(auto pair: pairs)\n {\n int u = pair[0];\n int v = pair[1];\n\n addEdge(u, v);\n }\n\n for(int i = 0; i < n; ++i)\n {\n if(!visited[i])\n {\n vector<int> component = bfs(i);\n\n vector<char> chars;\n // Thêm ký tự tương ứng từ chuỗi `s` vào vector `chars`\n for (auto idx : component) {\n chars.push_back(s[idx]);\n }\n\n // Sắp xếp các chỉ số và ký tự\n sort(component.begin(), component.end());\n sort(chars.begin(), chars.end());\n\n // Đặt lại các ký tự đã sắp xếp vào vị trí tương ứng trong chuỗi `s`\n for (int j = 0; j < component.size(); ++j) {\n int idx = component[j];\n s[idx] = chars[j];\n }\n\n }\n }\n\n return s;\n }\n};\n",
"memory": "84162"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n int n;\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n n = s.length();\n vector<int> adj[n];\n for(int i=0;i<pairs.size();i++){\n adj[pairs[i][0]].push_back(pairs[i][1]);\n adj[pairs[i][1]].push_back(pairs[i][0]);\n }\n vector<int> vis(n,0);\n for(int i=0;i<n;i++){\n if(!vis[i]){\n vis[i] = 1;\n queue<int> q;\n q.push(i);\n map<int,int> mp;\n string st = \"\";\n while(!q.empty()){\n int node = q.front();\n q.pop();\n st += s[node];\n mp[node]++;\n for(auto it:adj[node]){\n if(!vis[it]){\n vis[it] = 1;\n q.push(it);\n }\n }\n }\n sort(st.begin(),st.end());\n int j = 0;\n for(auto it:mp){\n s[it.first] = st[j];\n j++;\n }\n }\n }\n return s;\n }\n};",
"memory": "84162"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector <char> allchar;\n vector <int> pos;\n string str;\n void dfs(int node, vector<bool>& vst, map<int, vector<int>>& adj) {\n vst[node] = true;\n allchar.push_back(str[node]);\n pos.push_back(node);\n for (auto itr : adj[node]) {\n if (!vst[itr]) {\n dfs(itr, vst, adj);\n }\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n map<int ,vector<int>> adj;\n for (auto itr: pairs)\n {\n adj[itr[0]].push_back(itr[1]);\n adj[itr[1]].push_back(itr[0]);\n }\n str=s;\n int n=s.length();\n vector <bool> vst(n,0);\n for (int i=0;i<n;i++)\n {\n if (!vst[i])\n {\n allchar.clear();\n pos.clear();\n dfs(i,vst,adj);\n sort(pos.begin(),pos.end());\n sort(allchar.begin(),allchar.end());\n for (int j=0;j<pos.size();j++)\n {\n str[pos[j]]=allchar[j];\n }\n \n }\n }\n return str;\n }\n};",
"memory": "84687"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n vector<vector<int>> adj(n,vector<int>());\n for(auto it:pairs){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<int> vis(n,0);\n vector<char> ans(n);\n for(int i=0; i<n; i++){\n if(!vis[i]){\n vector<int> es; //indices of the CC\n bfs(i , adj , es , vis);\n sort(es.begin() , es.end());\n vector<char> c; //characters at the indices of a CC\n for(auto it:es){\n c.push_back(s[it]); \n }\n // sorted letters mapped to sorted index sequence of the CC\n sort(c.begin() , c.end());\n int k = 0;\n for(auto it:es){\n ans[it] = c[k]; k++;\n }\n }\n }\n string finn = \"\";\n for(int i=0; i<n; i++){\n finn += ans[i];\n }\n return finn;\n }\n void bfs(int st, vector<vector<int>>& adj, vector<int>& es , vector<int>& vis){\n queue<int> q;\n q.push(st);\n vis[st] = 1;\n es.push_back(st);\n while(!q.empty()){\n int par = q.front();\n q.pop();\n for(auto it:adj[par]){\n if(!vis[it]){\n vis[it] = 1;\n q.push(it);\n es.push_back(it);\n }\n }\n }\n }\n};",
"memory": "85212"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n vector<int> bfs(int root,vector<vector<int>> &adj,vector<bool> &vis){\n queue<int> q;\n q.push(root);\n vis[root]=true;\n vector<int> ans;\n ans.push_back(root);\n while(!q.empty()){\n int node=q.front();\n q.pop();\n for(auto it:adj[node]){\n if(!vis[it]){\n vis[it]=true;\n ans.push_back(it);\n q.push(it);\n }\n }\n }\n return ans; \n }\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.size();\n vector<vector<int>> adj(n);\n for(auto it:pairs){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<bool> vis(n,false);\n vector<vector<int>> store;\n for(int i=0;i<n;i++){\n if(!vis[i]){\n store.push_back(bfs(i,adj,vis));\n }\n }\n for(auto it:store){\n sort(it.begin(),it.end());\n string temp=\"\";\n for(auto i:it){\n // cout<<i<<\" \"<<s[i]<<\" \";\n temp+=s[i];\n }\n // cout<<endl;\n sort(temp.begin(),temp.end());\n int t=0;\n for(auto i:it){\n s[i]=temp[t++];\n }\n }\n return s;\n }\n};",
"memory": "85737"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(int node, vector<vector<int>>& adj, set<int>& st, set<int>& visited) {\n visited.insert(node);\n st.insert(node);\n for (auto it : adj[node]) {\n if (visited.find(it) == visited.end()) {\n dfs(it, adj, st, visited);\n }\n }\n }\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n vector<vector<int>> adj(n);\n set<int> visited;\n\n // Construct the graph (undirected)\n for (auto& it : pairs) {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]); // Ensure bidirectional edge\n }\n\n string ans = s;\n\n // Traverse all nodes\n for (int i = 0; i < n; ++i) {\n if (visited.find(i) == visited.end()) {\n set<int> st;\n dfs(i, adj, st, visited);\n\n // Extract characters and sort them\n string temp = \"\";\n for (auto idx : st) {\n temp += s[idx];\n }\n sort(temp.begin(), temp.end());\n\n // Put sorted characters back in their positions\n auto it = temp.begin();\n for (auto idx : st) {\n ans[idx] = *it;\n ++it;\n }\n }\n }\n\n return ans;\n }\n};\n",
"memory": "85737"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n unordered_map<int,int> location;\n vector<vector<int>> comp;\n vector<int> visited;\npublic:\n void dfs(int node, int compNo,vector<vector<int>>& adj,string& s){\n visited[node]=1;\n location[node]=compNo;\n comp[compNo].push_back(s[node]);\n\n for(int e:adj[node]){\n if(!visited[e]){\n dfs(e,compNo,adj,s);\n }\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.size();\n vector<vector<int>> adj(n);\n for(auto it:pairs){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n\n visited.resize(n,0);\n\n int compNo=0;\n for(int i=0;i<n;i++){\n if(!visited[i]){\n comp.push_back({});\n dfs(i,compNo,adj,s);\n compNo++;\n }\n }\n\n for(int i=0;i<comp.size();i++){\n sort(comp[i].rbegin(),comp[i].rend());\n }\n\n string res=\"\";\n for(int i=0;i<n;i++){\n res += comp[location[i]].back();\n comp[location[i]].pop_back();\n }\n\n return res;\n\n }\n};",
"memory": "86262"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int find(int a,vector<int>& parent)\n {\n if(parent[a]==a) return a;\n int ans=find(parent[a],parent);\n parent[a]=ans;\n return ans;\n }\n void combine(int a,int b,vector<int>& parent,vector<int>& rank,vector<vector<int>>& pos,vector<vector<char>>& chars)\n {\n int p1=find(a,parent),p2=find(b,parent);\n\n if(p1==p2) return;\n\n if(rank[p1]<rank[p2])\n {\n parent[p1]=p2;\n vector<int> dup1=pos[p1];\n vector<char> dup2=chars[p1];\n\n int len=dup1.size();\n\n for(int i=0;i<len;i++)\n {\n pos[p2].push_back(dup1[i]);\n chars[p2].push_back(dup2[i]);\n }\n }\n else if(rank[p2]<=rank[p1])\n {\n parent[p2]=p1;\n vector<int> dup1=pos[p2];\n vector<char> dup2=chars[p2];\n\n int len=dup1.size();\n\n for(int i=0;i<len;i++)\n {\n pos[p1].push_back(dup1[i]);\n chars[p1].push_back(dup2[i]);\n }\n\n if(rank[p2]==rank[p1]) rank[p1]++;\n }\n }\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n \n int n=s.length();\n \n vector<vector<int>> pos(n);\n vector<vector<char>> chars(n);\n vector<int> parent(n);\n vector<int> rank(n);\n\n for(int i=0;i<n;i++) rank[i]=1;\n\n iota(parent.begin(),parent.end(),0);\n for(int i=0;i<n;i++)\n {\n pos[i].push_back(i);\n chars[i].push_back(s[i]);\n }\n\n for(auto a:pairs) \n {\n combine(a[0],a[1],parent,rank,pos,chars);\n }\n\n for(int i=0;i<n;i++)\n {\n if(parent[i]==i)\n {\n sort(pos[i].begin(),pos[i].end());\n sort(chars[i].begin(),chars[i].end());\n int len=pos[i].size();\n\n for(int j=0;j<len;j++) s[pos[i][j]]=chars[i][j];\n }\n }\n return s;\n }\n};",
"memory": "86262"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nvoid dfs(int i , multiset<int> &index , multiset<char> &val ,string &s ,vector<int> &vis ,vector<int> adj[]) {\n vis[i] = 1 ;\n index.insert(i) ;\n val.insert(s[i]) ;\n for(auto el : adj[i]){\n if(!vis[el]){\n dfs(el ,index,val,s,vis,adj) ;\n }\n }\n}\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n string ans = s ;\n vector<int> adj[s.length()] ;\n for(int i=0 ; i<pairs.size() ; i++){\n adj[pairs[i][0]].push_back(pairs[i][1]) ;\n adj[pairs[i][1]].push_back(pairs[i][0]) ;\n }\n vector<int> vis(s.length() ,0) ;\n for(int i=0 ; i<s.length() ; i++){\n multiset<int> index ;\n multiset<char> val ;\n if(!vis[i]){\n dfs(i ,index,val,s,vis,adj) ;\n }\n auto it1 = index.begin();\n auto it2 = val.begin();\n\n while (it1 != index.end() && it2 != val.end()) {\n ans[*it1] = *it2 ;\n ++it1;\n ++it2;\n }\n }\n return ans ;\n }\n};",
"memory": "86787"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n \n int n=s.length();\n vector<vector<int>> adjList(n);\n\n int len=pairs.size();\n\n for(auto a:pairs)\n {\n adjList[a[0]].push_back(a[1]);\n adjList[a[1]].push_back(a[0]);\n }\n\n vector<vector<int>> comps;\n\n vector<bool> visited(n,false);\n\n for(int i=0;i<n;i++)\n {\n if(!visited[i])\n {\n visited[i]=true;\n queue<int> q;\n q.push(i); \n vector<int> dup;\n\n while(q.size()>0)\n {\n auto top=q.front();\n q.pop();\n dup.push_back(top);\n \n for(auto a:adjList[top])\n {\n if(!visited[a])\n {\n visited[a]=true;\n q.push(a);\n }\n }\n }\n\n sort(dup.begin(),dup.end());\n comps.push_back(dup);\n }\n }\n\n for(auto a:comps)\n {\n vector<char> dup;\n\n for(auto b:a)\n {\n dup.push_back(s[b]);\n }\n sort(dup.begin(),dup.end());\n\n int len=a.size();\n for(int i=0;i<len;i++)\n {\n s[a[i]]=dup[i];\n }\n }\n return s; \n }\n};",
"memory": "86787"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n const int n = s.size();\n vector<vector<int>> edges(n);\n for (const auto& p : pairs) {\n edges[p[0]].push_back(p[1]);\n edges[p[1]].push_back(p[0]);\n }\n string smallest = s;\n unordered_set<int> visited;\n for (int from = 0; from < n; ++from) {\n //if (visited.count(from)) continue;\n vector<char> chars;\n vector<int> indexes;\n function<void(int)> dfs = [&](int from) {\n if (visited.count(from)) return;\n visited.insert(from);\n chars.push_back(s[from]);\n indexes.push_back(from);\n for (auto to : edges[from]) {\n dfs(to);\n }\n };\n dfs(from);\n sort(chars.begin(), chars.end());\n sort(indexes.begin(), indexes.end());\n for (int i = 0; i < chars.size(); ++i) {\n smallest[indexes[i]] = chars[i];\n }\n }\n return smallest;\n }\n};\n\n/*\n\ndcab\n\n0,3\n1,2\n\nbd ac\nbacd\n\ncollecting the pairs in a group\n dfs\n visited indexes\nsort the indexes in the group\n\n*/",
"memory": "87312"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(string& s, vector<int> adj[],int curr,vector<int>& index,vector<char>& c,vector<int>& vis){\n vis[curr] = 1;\n index.push_back(curr);\n c.push_back(s[curr]);\n for(auto x: adj[curr]){\n if(vis[x]) continue;\n dfs(s,adj,x,index,c,vis);\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.length();\n vector<int> vis(n,0);\n vector<int> adj[n];\n for(auto x: pairs){\n adj[x[0]].push_back(x[1]);\n adj[x[1]].push_back(x[0]);\n }\n for(auto x:pairs){\n vector<int> index;\n vector<char> c;\n dfs(s,adj,x[0],index,c,vis);\n sort(index.begin(),index.end());\n sort(c.begin(),c.end());\n for(int i=0;i<index.size();i++){\n s[index[i]] = c[i];\n }\n }\n\n return s ;\n }\n};",
"memory": "87837"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(int node,map<int,vector<int>> &g,vector<bool> &vis,vector<int> &c){\n if(vis[node]){\n return ; \n }\n\n c.push_back(node); \n vis[node] = true; \n for(int adj : g[node]){\n dfs(adj,g,vis,c); \n }\n\n\n return ; \n\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n map<int,vector<int>> g; \n\n for(int i = 0;i<pairs.size();i++){\n g[pairs[i][0]].push_back(pairs[i][1]); \n g[pairs[i][1]].push_back(pairs[i][0]);\n }\n\n vector<bool> vis(s.size(),false); \n for(int i = 0;i<s.size();i++){\n vector<int> c; \n if(not vis[i]){\n dfs(i,g,vis,c); \n }\n multiset<int> se; \n for(int i : c){\n se.insert(s[i]); \n }\n sort(c.begin(),c.end()); \n int cnt = 0; \n for(int i1 : se){\n s[c[cnt]] = i1; \n cnt += 1; \n }\n }\n\n\n return s; \n\n\n }\n};",
"memory": "87837"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(int start, std::unordered_map<int, vector<int>>& mp, std::vector<int>& component, vector<bool>& visited) {\n if (visited[start]) return;\n visited[start] = true;\n component.push_back(start);\n vector<int> nodes = mp[start];\n for (int i = 0; i < nodes.size(); i++) {\n if (!visited[nodes[i]]) {\n dfs(nodes[i], mp, component, visited);\n }\n }\n }\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n std::unordered_map<int, vector<int>> mp;\n for (int i = 0; i < pairs.size(); i++) {\n mp[pairs[i][0]].push_back(pairs[i][1]);\n mp[pairs[i][1]].push_back(pairs[i][0]);\n }\n\n vector<bool> visited(s.size(), false);\n for (int i = 0; i < s.size(); i++) {\n if (!visited[i]) {\n std::vector<int> component;\n dfs(i, mp, component, visited);\n\n // Sort indices and corresponding characters\n std::vector<char> chars;\n for (int idx : component) {\n chars.push_back(s[idx]);\n }\n sort(component.begin(), component.end());\n sort(chars.begin(), chars.end());\n\n // Place the sorted characters back in the original string\n for (int j = 0; j < component.size(); j++) {\n s[component[j]] = chars[j];\n }\n }\n }\n return s;\n }\n};\n",
"memory": "88362"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> dfs(int u, vector<int>graph[], vector<bool>& visit) \n {\n vector<int> cmb;\n stack<int> S;\n S.push(u);\n visit[u] = true;\n cmb.push_back(u);\n while(!S.empty()) \n {\n int uu = S.top();\n S.pop();\n for(auto v: graph[uu]) \n {\n if(visit[v] == false) \n {\n visit[v] = true;\n S.push(v);\n cmb.push_back(v);\n }\n }\n }\n return cmb;\n } \n \n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) \n {\n int n = s.size();\n vector<int> adj[n];\n for(auto it: pairs)\n {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n\n vector<vector<int>> connected;\n vector<bool> vis(n,false);\n\n for(int i = 0;i<n;i++)\n {\n if(!vis[i])\n connected.push_back(dfs(i,adj,vis));\n }\n \n string ans = s;\n for(auto it: connected)\n {\n vector<int> charpos;\n vector<char> character;\n for(int i = 0;i<it.size();i++)\n {\n charpos.push_back(it[i]);\n character.push_back(s[it[i]]);\n }\n\n sort(charpos.begin(),charpos.end());\n sort(character.begin(),character.end());\n\n for(int i=0;i<it.size();i++)\n ans[charpos[i]] = character[i];\n }\n return ans;\n }\n};",
"memory": "88362"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n unordered_map<int, vector<int>> adj_list;\n\n for(auto& edge : pairs)\n {\n adj_list[edge[0]].emplace_back(edge[1]);\n adj_list[edge[1]].emplace_back(edge[0]);\n }\n\n string result(s.size(), ' ');\n\n unordered_set<int> visit;\n \n\n for(int idx = 0; idx < s.size(); idx++)\n {\n vector<char> chars;\n vector<int> indices;\n if(DFS(idx, s, adj_list, visit, chars, indices))\n {\n sort(chars.begin(), chars.end());\n sort(indices.begin(), indices.end());\n for(int i = 0; i < indices.size(); i++)\n {\n result[indices[i]] = chars[i];\n }\n }\n }\n\n return result;\n // for(auto& edge : adj_list)\n // {\n // stack<int> s{edge.first};\n // vector<char> chars;\n // vector<int> indices;\n // while(!s.empty())\n // {\n // int cur = s.top();\n // s.pop();\n // if(visit.count(cur))\n // {\n // continue;\n // }\n\n // }\n // }\n \n\n //s.push(adj_list.front());\n }\n\n bool DFS(int cur, string& input, unordered_map<int, vector<int>>& adj_list, unordered_set<int>& visit, vector<char>& chars, vector<int>& indices)\n {\n if(visit.count(cur))\n {\n return false;\n }\n\n visit.insert(cur);\n chars.push_back(input[cur]);\n indices.push_back(cur);\n\n\n for(auto& edge : adj_list[cur])\n {\n DFS(edge, input, adj_list, visit, chars, indices);\n }\n\n return true;\n }\n};",
"memory": "88887"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> adj[100001];\n bool visited[100001];\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n for(int i=0; i<pairs.size(); i++) {\n int a = pairs[i][0];\n int b = pairs[i][1];\n adj[a].push_back(b);\n adj[b].push_back(a);\n }\n\n vector<vector<int>> connected;\n\n for(int i=0; i<n; i++) {\n if(visited[i]) continue;\n vector<int> component;\n queue<int> q;\n q.push(i);\n while(!q.empty()) {\n int cur = q.front(); q.pop();\n visited[cur] = true;\n component.push_back(cur);\n for(int next : adj[cur]) {\n if(!visited[next]) {\n q.push(next);\n visited[next] = true;\n }\n }\n }\n connected.push_back(component);\n }\n\n unordered_map<int, char> map;\n\n for(int i=0; i<connected.size(); i++) {\n vector<int> component = connected[i];\n string curString;\n for(int index : component) {\n curString += s[index];\n }\n sort(curString.begin(), curString.end());\n sort(component.begin(), component.end());\n for(int j=0; j<component.size(); j++) {\n map[component[j]] = curString[j];\n }\n }\n\n string ans;\n for(int i=0; i<n; i++) {\n if(map[i])\n ans += map[i];\n else\n ans += s[i];\n }\n return ans;\n }\n};",
"memory": "88887"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n void dfs(int n, vector<vector<int>> &adj, vector<int> &vis, string &s, multiset<char> &s1, set<int> &indices){\n vis[n] = 1;\n s1.insert(s[n]);\n indices.insert(n);\n for(auto it: adj[n]){\n if(!vis[it])\n dfs(it,adj, vis, s, s1, indices);\n }\n }\n\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n string ans = s;\n vector<vector<int>> adj(n);\n for(auto it: pairs){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<int> vis(n,0);\n multiset<char> s1;\n set<int> indices;\n for(int i = 0; i<n; i++){\n if(!vis[i]){\n dfs(i, adj, vis, s, s1, indices);\n }\n while(!s1.empty()){\n ans[*indices.begin()] = *s1.begin();\n indices.erase(indices.begin());\n s1.erase(s1.begin());\n }\n }\n return ans;\n \n }\n};",
"memory": "90987"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n void dfs(unordered_map<int,vector<int>>& graph,string& s,set<int>& idxs,vector<char>& chars,vector<bool>& visited, int idx){\n idxs.insert(idx);\n chars.push_back(s[idx]);\n\n for(int& next:graph[idx]){\n if(!visited[next]){\n visited[next]=true;\n dfs(graph,s,idxs,chars,visited,next);\n }\n }\n } \n void bucketSort(vector<char>& chars){\n int arr[26]={0};\n for(char& ch:chars) arr[ch-'a']++;\n for(int i=0,j=0;i<26;i++){\n while(arr[i]>0){\n chars[j++]=(char)i+97;\n arr[i]--;\n }\n }\n }\n void placeCharsInString(string& s, set<int>& idxs, vector<char>& chars){\n int i=0;\n for(set<int>::iterator itr=idxs.begin();itr!=idxs.end();itr++){\n s[*itr]=chars[i++];\n }\n }\npublic:\n string smallestStringWithSwaps(string& s, vector<vector<int>>& pairs) {\n unordered_map<int,vector<int>> graph;\n for(vector<int>& pair:pairs){\n graph[pair[0]].push_back(pair[1]);\n graph[pair[1]].push_back(pair[0]);\n }\n vector<bool> visited(s.size(),false);\n for(int i=0;i<s.size();i++){\n if(!visited[i]){\n visited[i]=true;\n set<int> idxs;\n vector<char> chars;\n dfs(graph,s,idxs,chars,visited,i);\n bucketSort(chars);\n placeCharsInString(s,idxs,chars);\n }\n }\n return s;\n }\n};",
"memory": "91512"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n vector<bool> visited(n, false); \n unordered_map<int, vector<int>> adj;\n for(auto v : pairs){\n adj[v[0]].push_back(v[1]);\n adj[v[1]].push_back(v[0]);\n }\n for(int i = 0; i < n; i++){\n if(visited[i]) continue;\n // BFS\n queue<int> nodes;\n nodes.push(i);\n visited[i] = true;\n vector<int> idx;\n vector<char> C;\n while(!nodes.empty()){\n int k = nodes.front();\n idx.push_back(k);\n C.push_back(s[k]);\n nodes.pop();\n for(auto t : adj[k]){\n if(!visited[t]){\n nodes.push(t);\n visited[t] = true;\n }\n }\n }\n \n sort(idx.begin(), idx.end());\n sort(C.begin(), C.end());\n \n for(int j = 0; j < idx.size(); j++){\n s[idx[j]] = C[j];\n }\n }\n return s;\n }\n};",
"memory": "92037"
} |
1,308 | <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p>
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p>
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10^5</code></li>
<li><code>0 <= pairs.length <= 10^5</code></li>
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
<li><code>s</code> only contains lower case English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n void dfs(unordered_map<int,vector<int>>& graph,string& s,set<int>& idxs,vector<char>& chars,vector<bool>& visited, int idx){\n idxs.insert(idx);\n chars.push_back(s[idx]);\n\n for(int& next:graph[idx]){\n if(!visited[next]){\n visited[next]=true;\n dfs(graph,s,idxs,chars,visited,next);\n }\n }\n } \n void bucketSort(vector<char>& chars){\n int arr[26]={0};\n for(char& ch:chars) arr[ch-'a']++;\n for(int i=0,j=0;i<26;i++){\n while(arr[i]>0){\n chars[j++]=(char)i+97;\n arr[i]--;\n }\n }\n }\n void placeCharsInString(string& s, set<int>& idxs, vector<char>& chars){\n bucketSort(chars);\n int i=0;\n for(set<int>::iterator itr=idxs.begin();itr!=idxs.end();itr++){\n s[*itr]=chars[i++];\n }\n }\npublic:\n string smallestStringWithSwaps(string& s, vector<vector<int>>& pairs) {\n unordered_map<int,vector<int>> graph;\n for(vector<int>& pair:pairs){\n graph[pair[0]].push_back(pair[1]);\n graph[pair[1]].push_back(pair[0]);\n }\n vector<bool> visited(s.size(),false);\n for(int i=0;i<s.size();i++){\n if(!visited[i]){\n visited[i]=true;\n set<int> idxs;\n vector<char> chars;\n dfs(graph,s,idxs,chars,visited,i);\n placeCharsInString(s,idxs,chars);\n }\n }\n return s;\n }\n};",
"memory": "92037"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.