id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
0
{ "code": "class Solution {\npublic:\n int par[26];\n \n int find(int x){\n if(par[x]==-1) return x;\n return par[x]=find(par[x]);\n }\n \n void Union(int x, int y) {\n x = find(x);\n y = find(y);\n \n if (x != y) \n par[max(x, y)] = min(x, y); \n...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
0
{ "code": "class UnionFind {\nprivate:\n int root[26] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,\n 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25};\npublic:\n int Find(int node)\n {\n if(root[node] != node)\n {\n root[node] = Find(root[node]);\n ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
0
{ "code": "class Solution {\n struct UnionFind {\n UnionFind() {\n std::iota(parents.begin(), parents.end(), 0);\n std::iota(chars.begin(), chars.end(), 0);\n }\n \n int find(int value) {\n if (parents[value] == value)\n return value;\n ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
0
{ "code": "class Solution {\n struct UnionFind {\n UnionFind() {\n std::iota(parents.begin(), parents.end(), 0);\n std::iota(chars.begin(), chars.end(), 0);\n }\n \n int find(int value) {\n if (parents[value] == value)\n return value;\n ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
0
{ "code": "class DisjointSet\n{\n\tpublic:\n\tvector<int> parent;\n\tvector<int> size;\n\n\tDisjointSet(int n)\n\t{\n\t\tparent.resize(n);\n\t\tsize.resize(n, 1);\n\n\t\tfor(int i=0; i<n; i++)\n\t\tparent[i] = i;\n\t}\n\n\tint findParent(int node)\n\t{\n\t\tif(node == parent[node])\n\t\treturn node;\n\n\t\telse\n\t\t...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
0
{ "code": "class Solution {\npublic:\n\nvector<int> headChar;\n\n//find parent\nint find(int x)\n{\n\tif(headChar[x]==-1)\n\treturn x;\n\treturn headChar[x]=find(headChar[x]);\n}\n\n//union \nvoid Union(int x,int y)\n{\n\tint parentX=find(x);\n\tint parentY=find(y);\n\t\n\tif(parentX==parentY)\n\treturn;\n\n\t//make ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
1
{ "code": "class Disjointset{\npublic:\n vector<int>size,parent;\n Disjointset (int n){\n size.resize(n);\n parent.resize(n);\n for(int i = 0; i < n; i++){\n size[i] = 1;\n parent[i] = i;\n }\n }\n\n int findUparent(int node){\n if(node == parent[no...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
1
{ "code": "class Solution {\npublic:\n char findParent(int child, vector<int> &parent) {\n if (child == parent[child]) {\n return child;\n }\n\n return parent[child] = findParent(parent[child], parent);\n }\n\n void merge(int c1, int c2, vector<int> &parent) {\n int p1 ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
1
{ "code": "class Solution {\n struct UnionFind {\n UnionFind() {\n std::iota(parents.begin(), parents.end(), 0);\n std::iota(chars.begin(), chars.end(), 0);\n }\n \n int find(int value) {\n if (parents[value] == value)\n return value;\n ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n// Node struct to represent a character and its properties in the Disjoint-Set\nstruct Node{\n int id =-1; // id of the character, initialize it to -1 to represent uninitialized state\n char label=' '; // label of the character, initialize it to ' ' to represent uninitializ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n class DisjointSet {\n private: \n vector<int> parent;\n public:\n DisjointSet(int n) : parent(n, 1) {\n for (int i = 0; i < n; ++i) {\n parent[i] = i;\n }\n }\n\n int FindRoot(int p) {\n int roo...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n void dfs(vector<char> adj[], char node, char minCharacter, vector<char>& visited)\n {\n // to this character, replace it with it's minimum character\n visited[node - 'a'] = minCharacter;\n \n // travel in neighbour of this character,\n //...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n \n\nvoid dfs(int node ,vector<int>&vis,vector<int>adj[],int &val,vector<int>&correspondence){\n\n vis[node]=1;\n val = min(val,node);\n for(auto &child : adj[node]){\n if(vis[child]) continue;\n\n dfs(child,vis,adj,val,correspondence);\n \n }\n correspondence[node...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n \n std::vector<std::vector<int>> adjList(26);\n for (int i = 0; i < s1.size(); ++i){\n adjList[s1[i] - 'a'].emplace_back(s2[i] - 'a');\n adjList[s2[i] - 'a'].e...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n \n std::vector<std::vector<int>> adjList(26);\n for (int i = 0; i < s1.size(); ++i){\n adjList[s1[i] - 'a'].emplace_back(s2[i] - 'a');\n adjList[s2[i] - 'a'].e...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\n private: \n void dfs(int par, int node , int minarr[] , int vis[] , vector<vector<int>> &adj)\n {\n vis[node] = 1;\n \n for(auto it : adj[node])\n {\n if(!vis[it] && it!=par && it!=node){\n dfs(node,it,minarr,vis,adj);\n ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "struct DisjointSetUnion {\n DisjointSetUnion(int _size): size(_size) {\n for (int i = 0; i < _size; ++i)\n parent.emplace_back(i);\n };\n void unionSets(int a, int b){\n a = getParent(a);\n b = getParent(b);\n if (a != b){\n if (size[a] < size[b])\...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "struct DisjointSetUnion {\n DisjointSetUnion(int _size): size(_size) {\n for (int i = 0; i < _size; ++i)\n parent.emplace_back(i);\n };\n void unionSets(int a, int b){\n a = getParent(a);\n b = getParent(b);\n if (a != b){\n if (size[a] < size[b])\...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "struct DisjointSetUnion {\n DisjointSetUnion(int _size): size(_size) {\n for (int i = 0; i < _size; ++i)\n parent.emplace_back(i);\n };\n void unionSets(int a, int b){\n a = getParent(a);\n b = getParent(b);\n if (a != b){\n if (size[a] < size[b])\...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\n \n struct unionNode {\n \n int parent ; \n int rank = 1 ;\n };\n \n struct unionFind {\n \n unionNode charNodes[26];\n \n int returnParent ( const int & a ) {\n int x = charNodes[a].parent;\n if ( x != ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n vector<int> parent;\n vector<int> rank;\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n = s1.size();\n parent.resize(26);\n rank.resize(26);\n for(int i = 0;i<26;i++){\n parent[i] = i;\n ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n\n int findUPar(int node,vector<int> &parent)\n {\n if(parent[node]==node)return node;\n return parent[node]=findUPar(parent[node],parent);\n }\n void unionBySize(vector<int> &size,int node1,int node2,vector<int> &parent)\n {\n int ulPar1=findU...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n vector<int> rank, par;\n int find(int x){\n if(x == par[x]) return x;\n return par[x] = find(par[x]);\n }\n\n void union_(int x, int y){\n int parx = find(x), pary = find(y);\n if(parx == pary) return;\n if(rank[parx]> rank[pary]){\...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n vector<int> rank, par;\n int find(int x){\n if(x == par[x]) return x;\n return par[x] = find(par[x]);\n }\n\n void union_(int x, int y){\n int parx = find(x), pary = find(y);\n if(parx == pary) return;\n if(rank[parx]> rank[pary]){\...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n vector<int> parent;\n vector<int> rank;\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n int n1 = s1.size(), n2 = baseStr.size();\n for(int i = 0;i<26;i++){\n parent.push_back(i);\n rank.push_back(0);\n ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Disjointset{\n vector<int> parent, size; \npublic: \n Disjointset(int n){\n parent.resize(n+1);\n size.resize(n+1);\n for(int i=0;i<=n;i++){\n size[i]=1;\n parent[i]=i;\n }\n \n }\n int findupar(int u){\n if(u==parent[u])\n retu...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n class UnionSet{\n public:\n vector<int> par;\n vector<int> siz;\n UnionSet(int n)\n {\n par.resize(n);\n siz.resize(n, 1);\n for(int i=0; i<n; i++)\n par[i]=i;\n...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n vector<int> parent;\n vector<int> rank;\n void Union(int x,int y){\n int x_parent=find(x);\n int y_parent=find(y);\n if(x_parent==y_parent)return ;\n if(rank[x_parent] < rank[y_parent]){\n parent[x_parent]=y_parent;\n }else ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n map<char,vector<char>>mp;\n\n for (char c = 'a'; c <= 'z'; c++) {\n mp[c].push_back(c);\n }\n\n\n for(int h=0;h<3;h++){\n for(int i=0;i<s1.size();i++){\...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n vector<int> alpha(26,-1);\n function<int(int)> get=[&](int ind){\n return (alpha[ind]<0?ind:alpha[ind]=get(alpha[ind]));\n };\n function<bool(int,int)> add=[&](int...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n vector<int> alpha(26,-1);\n function<int(int)> get=[&](int ind){\n return (alpha[ind]<0?ind:alpha[ind]=get(alpha[ind]));\n };\n function<bool(int,int)> add=[&](int...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n std::string smallestEquivalentString(std::string s1, std::string s2, std::string baseStr) {\n std::vector<char> map;\n std::vector<std::set<int>> tmpMap;\n tmpMap.resize(26);\n for (int i = 0; i < 26; i++) {\n tmpMap[i].insert(i);\n ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n std::string smallestEquivalentString(std::string s1, std::string s2, std::string baseStr) {\n std::vector<char> map;\n std::vector<std::set<int>> tmpMap;\n tmpMap.resize(26);\n for (int i = 0; i < 26; i++) {\n tmpMap[i].insert(i);\n ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n std::string smallestEquivalentString(std::string s1, std::string s2, std::string baseStr) {\n std::vector<char> map;\n std::vector<std::set<int>> tmpMap;\n tmpMap.resize(26);\n for (int i = 0; i < 26; i++) {\n tmpMap[i].insert(i);\n ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\n char dfs(char currChar, vector<vector<char>>& adj, vector<int>& visited) {\n visited[currChar - 'a'] = 1;\n char minChar = currChar;\n for (auto &neighbour : adj[currChar - 'a']) {\n if (!visited[neighbour - 'a']) {\n minChar = min(minCha...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
2
{ "code": "class Solution {\npublic:\n char dfs(char s, vector<int> adj[], vector<int>& vis) {\n vis[s - 'a'] = 1; // Mark the current character as visited\n char mini = s; // Initialize the smallest character as the current one\n \n for (auto it : adj[s - 'a']) {\n if (!vi...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "// class DisjointSet {\n// vector<int> rank, parent, size;\n// public:\n// DisjointSet(int n) {\n// rank.resize(n + 1, 0);\n// parent.resize(n + 1);\n// size.resize(n + 1);\n// for (int i = 0; i <= n; i++) {\n// parent[i] = i;\n// size[i] = 1;...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "#include <vector>\n#include <string>\n#include <algorithm>\n\nusing namespace std;\n\nclass Solution {\npublic:\n void dfs(int node, int parent, vector<vector<int>>& adj, vector<bool>& visited, int& minChar) {\n visited[node] = true;\n minChar = min(minChar, node);\n\n for (int neig...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class Solution {\npublic:\n\n char dfs(map<char, vector<char>>& adj, char current, vector<bool>& visited) {\n visited[current - 'a'] = true; // Mark current node as visited\n char minchar = current; // Initialize with the current character\n \n for (auto it : adj[current]) ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class Solution{\npublic:\n\n void dfs(char u, unordered_map<char, vector<char>>& adj, vector<bool>& vis, char& minChar){\n vis[u-'a']=true;\n minChar=min(minChar, u);\n for(char v: adj[u]){\n if(!vis[v-'a']){\n dfs(v, adj, vis, minChar);\n }\n }\n}\n\n strin...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class Solution {\npublic:\n\n void dfs(char u, unordered_map<char, vector<char>>& adj, vector<bool>& vis, char& ch){\n vis[u-'a']=true;\n ch=min(u, ch);\n for(auto v: adj[u]){\n if(!vis[v-'a']){\n dfs(v, adj, vis, ch);\n }\n }\n\n }\n \n ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class Solution {\npublic:\n\n void dfs(char u, unordered_map<char, vector<char>>& adj, vector<bool>& vis, char& ch){\n vis[u-'a']=true;\n ch=min(u, ch);\n for(auto v: adj[u]){\n if(!vis[v-'a']){\n dfs(v, adj, vis, ch);\n }\n }\n\n }\n \n ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class Solution{\npublic:\n\n //DFS appraoch: \n void dfs(char u, unordered_map<char, vector<char>>& adj, vector<bool>& vis, char& minChar){\n vis[u-'a']=true;\n minChar=min(minChar, u);\n for(char v: adj[u]){\n if(!vis[v-'a']){\n dfs(v, adj, vis, minChar);\n }\n...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class Solution {\npublic:\n void dfs(unordered_map<char,vector<char>>&adj,string &basestr,char &ch,vector<bool>&vis,char &mini){\n vis[ch-'a']=1;\n mini=min(mini,ch);\n\n for(char &it:adj[ch]){\n if(vis[it-'a']) continue;\n dfs(adj,basestr,it,vis,mini);\n ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class Solution {\npublic:\n \n char DFS(unordered_map<char, vector<char>> &adj, char curr, vector<int>& visited) {\n visited[curr-'a'] = 1;\n \n char minChar = curr;\n \n for(char &v : adj[curr]) {\n \n if(visited[v-'a'] == 0)\n ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class DSU{\n private:\n vector<int>parent;\n int n;\n\n public:\n DSU()\n {\n n=26;\n parent.resize(26,0);\n\n for(int i=0;i<26;i++)\n parent[i]=i;\n }\n\n int find(int i)\n {\n if(parent[i]==i...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class DSU{\n private:\n vector<int>parent;\n int n;\n\n public:\n DSU()\n {\n n=26;\n parent.resize(26,0);\n\n for(int i=0;i<26;i++)\n parent[i]=i;\n }\n\n int find(int i)\n {\n if(parent[i]==i...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class Solution {\npublic:\n char DFS_find_min_char(unordered_map<char,vector<char>>&adj,char curr_ch,vector<int>&visited){\n visited[curr_ch-'a']=1;//mark it visited;\n char minChar=curr_ch;\n for(char &v:adj[curr_ch]){\n if(visited[v-'a']==0){\n minChar=mi...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class Solution {\npublic:\n char DFS_find_min_char(unordered_map<char,vector<char>>&adj,char curr_ch,vector<int>&visited){\n visited[curr_ch-'a']=1;//mark it visited;\n char minChar=curr_ch;\n for(char &v:adj[curr_ch]){\n if(visited[v-'a']==0){\n minChar=mi...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class Solution {\npublic:\n char DFS(unordered_map<char, vector<char>> &adj, char curr, vector<int>& visited) {\n visited[curr-'a'] = 1;\n \n char minChar = curr;\n \n for(char &v : adj[curr]) {\n \n if(visited[v-'a'] == 0)\n minCha...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class Solution {\npublic:\n int dfs(int index, vector<int>& visited,unordered_map<int, vector<int>>& adjList) {\n visited[index] = 1;\n int min_char = index;\n for (auto& v : adjList[min_char]) {\n if (visited[v] == 0) {\n min_char = min(min_char, dfs(v, vi...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class Solution {\npublic:\n char DFS( char ch , vector<int> &vis , unordered_map<char,set<char>> &adj )\n {\n vis[ch-'a'] = 1;\n char minChar = ch;\n for( auto it : adj[ch] )\n {\n if( vis[it - 'a'] == -1 )\n {\n ch = min( ch , DFS(it , ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class Solution {\npublic:\n char dfs(unordered_map<char,list<char>>&mp,char cur_ch ,vector<bool>&visited){\n visited[cur_ch-'a'] =1;\n char minch =cur_ch;\n for(auto nbr :mp[cur_ch]){\n if(!visited[nbr-'a']){\n minch =min(minch,dfs(mp,nbr,visited));\n ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class Solution {\npublic:\n string smallestEquivalentString(string s1, string s2, string baseStr) {\n vector<vector<int>> edges(26);\n map<pair<char,char>,bool> m;\n for (int i=0;i<s1.size();i++)\n {\n if (m.find(pair(s1[i],s2[i])) == m.end())\n {\n ...
1,058
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have ...
3
{ "code": "class Solution {\npublic:\nchar dfs( unordered_map<char,list<char>>&adj,char ch,vector<int>&vis){\n vis[ch-'a']=1;\n char minc=ch;\n for(auto i:adj[ch]){\n if(!vis[i-'a']){\n minc=min(minc,dfs(adj,i,vis));\n }\n }\n return minc;\n}\n string smallestEquivalentString(st...
1,984
<p>You are given two <strong>non-increasing 0-indexed </strong>integer arrays <code>nums1</code>​​​​​​ and <code>nums2</code>​​​​​​.</p> <p>A pair of indices <code>(i, j)</code>, where <code>0 &lt;= i &lt; nums1.length</code> and <code>0 &lt;= j &lt; nums2.length</code>, is <strong>valid</strong> if both <code>i &lt;=...
0
{ "code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int res = 0;\n int i = 0, j = 0;\n while(i < nums1.size() && j < nums2.size()){\n while(j < nums2.size() && nums1[i] <= nums2[j]){\n j++;\n }\n i++...
1,984
<p>You are given two <strong>non-increasing 0-indexed </strong>integer arrays <code>nums1</code>​​​​​​ and <code>nums2</code>​​​​​​.</p> <p>A pair of indices <code>(i, j)</code>, where <code>0 &lt;= i &lt; nums1.length</code> and <code>0 &lt;= j &lt; nums2.length</code>, is <strong>valid</strong> if both <code>i &lt;=...
0
{ "code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n ptrdiff_t ans{0};\n auto it1{nums1.begin()}, it2{nums2.begin()};\n while (it1 != nums1.end() && it2 != nums2.end()) {\n if (*it1 > *it2) {\n ++it1;\n }\n ...
1,984
<p>You are given two <strong>non-increasing 0-indexed </strong>integer arrays <code>nums1</code>​​​​​​ and <code>nums2</code>​​​​​​.</p> <p>A pair of indices <code>(i, j)</code>, where <code>0 &lt;= i &lt; nums1.length</code> and <code>0 &lt;= j &lt; nums2.length</code>, is <strong>valid</strong> if both <code>i &lt;=...
0
{ "code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n const int n1 = nums1.size();\n const int n2 = nums2.size();\n auto i = 0;\n auto j = 0;\n auto ret = 0;\n while (i < n1 && j < n2) {\n if (nums1[i] <= nums2[j]) {\...
1,984
<p>You are given two <strong>non-increasing 0-indexed </strong>integer arrays <code>nums1</code>​​​​​​ and <code>nums2</code>​​​​​​.</p> <p>A pair of indices <code>(i, j)</code>, where <code>0 &lt;= i &lt; nums1.length</code> and <code>0 &lt;= j &lt; nums2.length</code>, is <strong>valid</strong> if both <code>i &lt;=...
0
{ "code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int i=0,j=0;\n int c=0;\n while(i<nums1.size() && j<nums2.size())\n {\n if(nums1[i]<=nums2[j]){\n c=max(c,j-i);\n j++;\n ...
1,984
<p>You are given two <strong>non-increasing 0-indexed </strong>integer arrays <code>nums1</code>​​​​​​ and <code>nums2</code>​​​​​​.</p> <p>A pair of indices <code>(i, j)</code>, where <code>0 &lt;= i &lt; nums1.length</code> and <code>0 &lt;= j &lt; nums2.length</code>, is <strong>valid</strong> if both <code>i &lt;=...
0
{ "code": "class Solution {\npublic:\n int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n int n = nums1.size(), m = nums2.size();\n int i = 0, j = 0, maxi = 0;\n while(i < n && j < m) {\n if(nums1[i] > nums2[j]) {\n i++;\n }\n else {\n ...
1,984
<p>You are given two <strong>non-increasing 0-indexed </strong>integer arrays <code>nums1</code>​​​​​​ and <code>nums2</code>​​​​​​.</p> <p>A pair of indices <code>(i, j)</code>, where <code>0 &lt;= i &lt; nums1.length</code> and <code>0 &lt;= j &lt; nums2.length</code>, is <strong>valid</strong> if both <code>i &lt;=...
0
{ "code": "class Solution {\npublic:\nint maxDistance(vector<int>& n1, vector<int>& n2) {\n\n int ans=0,k=min(n1.size(),n2.size());\n for(int i=0; i<k;i++){\n int l=i,r=n2.size()-1,m;\n while(l<=r){\n m=l+(r-l)/2;\n int t= n1[i];\n if(n2[m]>=t){\n ...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n vector<int> g[n];\r\n int indegree[n];\r\n int node_color[n][26];\r\n memset(indegree, 0, sizeof(indegree));\r\n memset(node_color, 0...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n\r\n int node_color[n][26];\r\n int indegree[n];\r\n memset(indegree, 0, sizeof(indegree));\r\n memset(node_color, 0, sizeof(node_color));\r\...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\npublic:\n vector<int> g[100005];\n int n, xl[100005], indeg[100005], f[100005], q[100005];\n string C;\n \n bool topo(){\n int head = 0, tail = 0, i, j;\n for (i = 0; i < n; ++i)\n if (indeg[i] == 0) xl[++tail] = i;\n while (head < tail){\n ...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\n\r\npublic:\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n\r\n int n = colors.size();\r\n\r\n vector<vector<int>> graph(n);\r\n\r\n int indegree[n];\r\n\r\n int freq[n][26];\r\n\r\n memset(freq, 0, sizeof(freq));\r\n\r...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "/*\r\nMistakes:\r\n1. renaming of same variable and func\r\n2. destructuring vector of size 2\r\n\r\na -> b\r\na -> c\r\n\r\n0/1\r\ndp(u):\r\n MAX(color[u] == current_color + dp(v)) for all v in children(u)\r\n\r\nvis[], 0, 1, 2\r\ndp[]\r\n\r\nif vis[0]\r\n run the dp\r\n if vis[1]\r\n then its -1\r\...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "/*\r\nMistakes:\r\n1. renaming of same variable and func\r\n2. destructuring vector of size 2\r\n\r\na -> b\r\na -> c\r\n\r\n0/1\r\ndp(u):\r\n MAX(color[u] == current_color + dp(v)) for all v in children(u)\r\n\r\nvis[], 0, 1, 2\r\ndp[]\r\n\r\nif vis[0]\r\n run the dp\r\n if vis[1]\r\n then its -1\r\...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "/*\r\nMistakes:\r\n1. renaming of same variable and func\r\n2. destructuring vector of size 2\r\n\r\na -> b\r\na -> c\r\n\r\n0/1\r\ndp(u):\r\n MAX(color[u] == current_color + dp(v)) for all v in children(u)\r\n\r\nvis[], 0, 1, 2\r\ndp[]\r\n\r\nif vis[0]\r\n run the dp\r\n if vis[1]\r\n then its -1\r\...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\n int indegree[100000];\r\n vector<int>next[100000];\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int ans = 1;\r\n for (auto edge:edges){\r\n int a = edge[0], b = edge[1];\r\n next[a].push_back(b);\r\n ...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\n int indegree[100000];\r\n vector<int>next[100000];\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int ans = 1;\r\n for (auto edge:edges){\r\n int a = edge[0], b = edge[1];\r\n next[a].push_back(b);\r\n ...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n=colors.size();\r\n vector<vector<int>> v(n);\r\n vector<int> ind(n),topo;\r\n for(auto &i:edges){\r\n v[i[0]].push_back(i[1]);\r\n ind[i[1]]++;\r\...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n\r\n\r\n int dp[100005][26];\r\n\r\n bool recStack[100005];\r\n\r\n bool vis[100005];\r\n\r\n void find(int source , vector<int> adj[],string& colors,bool vis[]){\r\n\r\n\r\n vis[source] = true;\r\n\r\n\r\n for(int j = 0;j<adj[source].size();j++){\r\...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "\r\n#define pb push_back\r\nclass Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n\r\n int n = colors.length();\r\n\r\n int deg[n];\r\n memset(deg,0,sizeof(deg));\r\n\r\n vector<int>adj[n];\r\n for(auto ele:edges)\r\n {\r\n int u = ele[0];i...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n int dp[100005][26]= {}, vis[100005]={},realvis[100005] = {};\r\n vector<int> grp[100005];\r\n bool dfs(int s, string& color){\r\n \r\n bool f = false ;\r\n vis[s] = 1;\r\n realvis[s] = 1;\r\n for(auto x : grp[s]){\r\n if(...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n=colors.size();\r\n vector<int> adj[n];\r\n vector<int> ind(n,0);\r\n for(auto it:edges){\r\n adj[it[0]].push_back(it[1]);\r\n ind[it[1]]++;\r\n ...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n int dp[100001][26];\r\n vector<int> outgoing[100001];\r\n\r\n int dfs(int start, vector <int> &vis, string &s) {\r\n \r\n // if(vis[start])\r\n // return -1;\r\n\r\n if(dp[start][0] != -1)\r\n return 0;\r\n\r\n for(int i=0;...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\n private: static const int N = 1e5 + 5;\r\npublic:\r\n\r\nint indegree[N];\r\nint dp[N][26];\r\nint vis[N] , anc[N];\r\nvector<int>gr[N];\r\n// int dfs(int src , int par){\r\n// vis[src] = 1;\r\n// for(auto i : gr[src]){\r\n// if (anc[i]) return false;\r\n// i...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n\r\nint mx = 0;\r\nint dp[100001][26]; \r\n\r\nvector<int> toposort(int n, vector<int> g[]) {\r\n vector<int> indeg(n, 0);\r\n for(int i = 0; i < n; i++) {\r\n for(auto& b: g[i]) {\r\n indeg[b]++;\r\n }\r\n }\r\n\r\n queue<int> q;\r\n f...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\npublic:\n int dp[100001][26];\n bool detectCycle = false;\n int maxColor = 1;\n void getLargestValue(int node,vector<int>&dfsVisited,vector<vector<int>>&adjacencyList,string &colors){\n if(dp[node][colors[node]-'a']!=-1) return ;\n \n dp[node][colors[node]...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n int solve(int color, string & s, vector<int> adj[], int n, int node, vector<int> & dp)\r\n {\r\n if(dp[node]!=-1)\r\n return dp[node];\r\n\r\n int ans=0;\r\n for(auto i: adj[node])\r\n {\r\n ans= max(ans, solve(color, s, adj,...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\npublic:\n int largestPathValue(string colors, vector<vector<int>>& edges) {\n // creating graph\n int n = colors.size();\n vector<vector<int>> adj = vector(n, vector<int>());\n vector<int> in(n, 0);\n for(auto &v : edges){\n adj[v[0]].push_...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n=colors.size();\r\n int m=edges.size();\r\n vector<int> adj[n];\r\n vector<int> indeg(n);\r\n for(int i=0;i<m;i++)\r\n {\r\n int u=edges[i][0];\...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n_nodes = colors.size();\r\n vector<vector<int>> adj(n_nodes); // adjacency list\r\n vector<int> indegree(n_nodes, 0); // indegree array\r\n\r\n // Construct the...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n // detect there is a cycle\r\n // crate a 2d array m x 26\r\n int n = colors.size();\r\n vector<int> indegree(n, 0);\r\n vector<int> outdegree(n, 0);\r\n int ar...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size(), m = edges.size();\r\n vector<vector<int>> g(n);\r\n vector<int> d(n); // d[i]表示i点的入度\r\n for (auto& e: edges) {\r\n int a = e[0], b = e[...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size(), m = edges.size();\r\n vector<vector<int>> g(n);\r\n vector<int> d(n); // d[i]表示i点的入度\r\n for (auto& e: edges) {\r\n int a = e[0], b = e[1];\...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n int dp[100100][26];\r\n void dfs(int node,vector<int>adj[],vector<int>&vis,string &s){\r\n vis[node]=1;\r\n for(auto it:adj[node]){\r\n if(!vis[it]){\r\n dfs(it,adj,vis,s);\r\n }\r\n for(int j=0;j<26;j++){\r...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n void dfs(int node, vector<int> list[], vector<int>& vis, vector<vector<int>>& m, vector<int>& val){\r\n vis[node] = 1;\r\n for(auto it : list[node]){\r\n if(vis[it]==0) dfs(it, list, vis, m, val);\r\n for(int i = 0;i<26;i++){\r\n ...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n\r\n void dfs(int u, vector<vector<int>> &adj, vector<vector<int>> &dp, string &colors){\r\n if(dp[u][0] != -1)return;\r\n\r\n for(int i=0;i<26;i++){\r\n dp[u][i] = 0;\r\n }\r\n\r\n // dp[u][colors[u] - 'a']++;\r\n for(auto ne ...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n\r\n bool dfsCheckCycle(int node, vector<int> adj[], vector<int>& visited, vector<int>& recStack) {\r\n visited[node] = 1; // Mark node as visited\r\n recStack[node] = 1; // Mark node as part of the recursion stack\r\n\r\n // Visit all neighbo...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n\r\n bool dfsCheckCycle(int node, vector<int> adj[], vector<int>& visited, vector<int>& recStack) {\r\n visited[node] = 1; // Mark node as visited\r\n recStack[node] = 1; // Mark node as part of the recursion stack\r\n\r\n // Visit all neighbo...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "// OJ: https://leetcode.com/contest/weekly-contest-240/problems/largest-color-value-in-a-directed-graph/\r\n// Author: github.com/lzl124631x\r\n// Time: O(V + E)\r\n// Space: O(V + E)\r\nclass Solution {\r\n typedef array<int, 26> T;\r\npublic:\r\n int largestPathValue(string C, vector<vector<int>>& ...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n unordered_map<char,int> umap;\r\n vector<int> topo_sort(vector<vector<int>>& adj ){\r\n // int count=0;\r\n // for(auto j:adj){\r\n // cout<<count++<<\" -> \";\r\n // for( auto i:j){\r\n // cout<<i<<\" \";\r\n /...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\nbool dfs(vector<vector<int>>& adj,int n,int cur,vector<int>&vis,vector<int>&temp)\r\n{\r\n vis[cur]=1;\r\n temp[cur]=1;\r\n for(int i=0;i<adj[cur].size();i++)\r\n {\r\n int node=adj[cur][i];\r\n if(temp[node]==1)return true;\r\n if(vis[node]==...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n\r\n bool dfsCheckCycle(int node, vector<int> adj[], vector<int>& visited, vector<int>& recStack) {\r\n visited[node] = 1; // Mark node as visited\r\n recStack[node] = 1; // Mark node as part of the recursion stack\r\n\r\n // Visit all neighbo...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n // int f(int node,vector<vector<int>> &dp,vector<int> adj[], vector<bool> &vis, string &colors)\r\n // {\r\n\r\n // }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n=colors.size();\r\n int m=edges.size();\r\n v...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "// Use reverse topo sort to pass on the hashmap\n// If already visited, record the maximum values\n// At the end, check for cycle\n\nclass Solution {\npublic:\n int largestPathValue(string colors, vector<vector<int>>& edges) {\n int res = 0, n = colors.size();\n vector<vector<int>> dp(n, v...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n bool hasCycle(int src, vector<int> adj[], vector<int>& mark) {\r\n mark[src] = 1;\r\n for(auto child: adj[src]) {\r\n if(mark[child] == 2) {continue;}\r\n if(mark[child] == 1 || hasCycle(child, adj, mark)) {return true;}\r\n }\r\...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
0
{ "code": "class Solution {\r\npublic:\r\n bool dfs(int node, vector<int>&vis, vector<int>&path_vis, vector<int>adj[]){\r\n vis[node] = 1;\r\n path_vis[node] = 1;\r\n bool flag = false;\r\n\r\n for(auto it : adj[node]){\r\n if(!vis[it]){\r\n flag |= dfs(it, vis...
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong...
1
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.length(); // n should be the number of nodes, not edges\r\n\r\n vector<int> indegree(n, 0);\r\n vector<vector<int>> adj(n);\r\n\r\n for(auto &edge : edges){\r...