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>&nbs... | 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 ... |
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>&nbs... | 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 ... |
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>&nbs... | 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 (in... |
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>&nbs... | 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 ... |
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>&nbs... | 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 ... |
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>&nbs... | 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 ... |
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>&nbs... | 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... |
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>&nbs... | 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 ... |
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>&nbs... | 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 ... |
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>&nbs... | 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 ro... |
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>&nbs... | 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 ... |
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>&nbs... | 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(paren... |
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>&nbs... | 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(i... |
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>&nbs... | 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(... |
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>&nbs... | 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... |
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>&nbs... | 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, vec... |
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>&nbs... | 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]... |
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>&nbs... | 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(... |
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>&nbs... | 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... |
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>&nbs... | 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(pa... |
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>&nbs... | 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 retur... |
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>&nbs... | 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... |
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>&nbs... | 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 = ... |
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>&nbs... | 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 djsFin... |
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>&nbs... | 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 ... |
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>&nbs... | 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 ... |
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>&nbs... | 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 ... |
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>&nbs... | 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 no... |
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>&nbs... | 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 ... |
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>&nbs... | 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 smalles... |
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>&nbs... | 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 smalles... |
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>&nbs... | 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, ad... |
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>&nbs... | 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) ... |
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>&nbs... | 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 th... |
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>&nbs... | 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(!visit... |
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>&nbs... | 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... |
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>&nbs... | 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... |
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>&nbs... | 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,v... |
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>&nbs... | 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 ... |
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>&nbs... | 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 df... |
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>&nbs... | 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 {\... |
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>&nbs... | 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 ... |
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>&nbs... | 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;... |
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>&nbs... | 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 smallestStringWith... |
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>&nbs... | 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... |
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>&nbs... | 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... |
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>&nbs... | 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 ... |
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>&nbs... | 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,ad... |
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>&nbs... | 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(ad... |
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>&nbs... | 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 smallestS... |
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>&nbs... | 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(... |
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>&nbs... | 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 ... |
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>&nbs... | 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,... |
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>&nbs... | 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 char... |
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>&nbs... | 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 ... |
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>&nbs... | 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 smallestStringWith... |
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>&nbs... | 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... |
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>&nbs... | 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;... |
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>&nbs... | 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 ... |
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>&nbs... | 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,... |
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>&nbs... | 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... |
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>&nbs... | 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 }... |
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>&nbs... | 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 ... |
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>&nbs... | 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[... |
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>&nbs... | 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... |
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>&nbs... | 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 ... |
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>&nbs... | 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 ... |
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>&nbs... | 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[... |
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>&nbs... | 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 ... |
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>&nbs... | 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&... |
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>&nbs... | 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<i... |
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>&nbs... | 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... |
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>&nbs... | 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 visi... |
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>&nbs... | 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[ind... |
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>&nbs... | 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[... |
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>&nbs... | 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 ... |
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>&nbs... | 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 ... |
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>&nbs... | 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])... |
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>&nbs... | 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... |
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>&nbs... | 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 vecto... |
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>&nbs... | 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(pai... |
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>&nbs... | 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 ... |
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>&nbs... | 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 ... |
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>&nbs... | 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.po... |
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>&nbs... | 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 ... |
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>&nbs... | 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... |
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>&nbs... | 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<vect... |
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>&nbs... | 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 ... |
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>&nbs... | 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 ... |
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>&nbs... | 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 }... |
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>&nbs... | 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,a... |
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>&nbs... | 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);... |
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>&nbs... | 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 (... |
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>&nbs... | 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 ... |
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>&nbs... | 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]);\... |
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>&nbs... | 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 ... |
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>&nbs... | 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, i... |
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>&nbs... | 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[nex... |
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>&nbs... | 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... |
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>&nbs... | 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[nex... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.