id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int dp[500][500];\n int func(int i,int j,string &word1,string word2){\n if(i<0){\n return j+1;\n }\n if(j<0){\n return i+1;\n }\n if(dp[i][j]!=-1){\n return dp[i][j];\n }\n int ans=INT_MIN;\n...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int dp[500][500];\n int func(int i,int j,string &word1,string word2){\n if(i<0){\n return j+1;\n }\n if(j<0){\n return i+1;\n }\n if(dp[i][j]!=-1){\n return dp[i][j];\n }\n int ans=INT_MIN;\n...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\n int dp[501][501];\npublic:\n int solve(int n, int m, string &w1, string &w2){\n if(n==0 && m==0) return 0;\n if(n==0) return m;\n if(m==0) return n;\n if(w1.substr(0,n)==w2.substr(0,m)){\n return 0;\n }\n if(dp[n][m]!=-1) return ...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int dp[501][501];\n int helper(string word1, string word2){\n \n if(word1.length()==0) return dp[word1.length()][word2.length()]=word2.length();\n if(word2.length()==0) return dp[word1.length()][word2.length()]=word1.length();\n\n if(dp[word1.le...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int solveRec(string &word1, string &word2, int i, int j){\n if(i>= word1.length()){\n return word2.length() - j;\n }\n\n if(j>= word2.length()){\n return word1.length() - i;\n }\n int ans = 0;\n if(word1[i] == w...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\nprivate:\n int f(int i,int j,vector<vector<int>> &dp,string &s,string t){\n if(i < 0 && j < 0) return 0;\n if(i < 0) return j+1;\n if(j < 0) return i+1;\n if(dp[i][j] != -1) return dp[i][j];\n if(s[i] == t[j]){\n dp[i][j] = f(i-1,j-1,dp,s,t...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n //recursion, top-down\n //time complexity: O(m*n)\n \n int helper(int indx1, int indx2, string word1, string& word2, vector<vector<int>>& v)\n {\n if (indx2<0 && indx1>=0)\n return indx1+1;\n\n if (indx2<0 && indx1<0)\n return ...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\nint min1(string &word1,string word2,int i,int j,vector<vector<int>>&dp){\n if(j<0){\n return i+1;\n }\n if(i<0){\n return j+1;\n }\n if(dp[i][j]!=-1){\n return dp[i][j];\n }\n if(word1[i]==word2[j]){\n return dp[i][j]=min1(word1,word2,i-1,j...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int find(string &word1,string word2,int start1,int start2,vector<vector<int>>&dp)\n {\n if(start1>=word1.length())\n {\n return word2.length()-start2;\n }\n if(start2>=word2.length())\n {\n return word1.length()-star...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "// DFS + DP\nclass Solution {\n int M;\n int N;\n\n vector<vector<int>> dp;\n\n int dfs(int i1, int i2, string& word1, string word2) {\n if (i1 == -1 && i2 == -1) {\n return 0;\n }\n if (i1 == -1) {\n return i2 + 1;\n }\n if (i2 == -1) {\...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\n int solve(string& word1,string word2,int d,int e,vector<vector<int>>&v){\nif(d<0){\n return e+1;\n}\nif(e<0){\n return d+1;\n}\nif(v[d][e]!=-1){\n return v[d][e];\n}\nint g=0;\nint h=0;\n\nif(word1[d]==word2[e]){\n g=solve(word1,word2,d-1,e-1,v);\n}\nelse{\nh=1+min(solve(w...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\nprivate:\n int solve(string& word1,string& word2,vector<vector<int>>&dp,int i,int j){\n if(i==word1.length()){\n return word2.length()-j;\n }\n if(j==word2.length()){\n return word1.length()-i;\n }\n if(dp[i][j]!=-1){\n ...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n\n int solve(string s, string t, vector<vector<int>>&v){\n if(s==\"\"){\n return t.length();\n }\n if(t==\"\"){\n return s.length();\n }\n int m=s.length();\n int n=t.length();\n if(v[m][n]!=-1)\n re...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int editDistance(string s1, string s2, vector<vector<int>>&mem)\n {\n //Write your code here\n if(s1.size() == 0) return s2.size();\n if(s2.size() == 0) return s1.size();\n if(mem[s1.size()][s2.size()] != -1) return mem[s1.size()][s2.size()];\n ...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int H(string word1, string word2,vector<vector<int>>&dp) {\n int n=word1.size();\n int m=word2.size();\n if(word2.size()==0&&word1.size()==0){\n return 0;\n }\n if(word2.size()==0&&word1.size()!=0){\n return word1.size(...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int minDistance(string word1, string word2) {\n memo.clear();\n return dfs(word1, word2);\n }\nprivate:\n unordered_map<string, unordered_map<string, int>> memo;\n\n int dfs(const string& word1, const string& word2) {\n // Base Case: when one of ...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "const int NONE = -1;\n\nclass Solution\n{\n\tint solve(\n\t\tconst std::string& from, std::size_t fromIdx,\n\t\tconst std::string& to, std::size_t toIdx,\n std::vector<std::vector<int>>& cache)\n\t{\n if(toIdx > to.size() || fromIdx > from.size())\n return std::numeric_limits<int>:...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "const int NONE = -1;\n\nclass Solution\n{\n\tint solve(\n\t\tconst std::string& from, std::size_t fromIdx,\n\t\tconst std::string& to, std::size_t toIdx,\n std::vector<std::vector<int>>& cache)\n\t{\n\t\tif(toIdx == to.size() && fromIdx == from.size())\n\t\t\treturn 0;\n\n assert(fromIdx <= f...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "const int NONE = -1;\n\nclass Solution\n{\n\tint solve(\n\t\tconst std::string& from, std::size_t fromIdx,\n\t\tconst std::string& to, std::size_t toIdx,\n std::vector<std::vector<int>>& cache)\n\t{\n if(toIdx > to.size() || fromIdx > from.size())\n return std::numeric_limits<int>:...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "const int NONE = -1;\n\nclass Solution\n{\n\tint solve(\n\t\tconst std::string& from, std::size_t fromIdx,\n\t\tconst std::string& to, std::size_t toIdx,\n std::vector<std::vector<int>>& cache)\n\t{\n if(toIdx > to.size() || fromIdx > from.size())\n return std::numeric_limits<int>:...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "/*\n\ndp(i,j)\n\nw1[i] != w2[i]\n if i + 1 > len(w1)\n replace\n \n else\n if w[i+1] == w2[i]\n remove\n else\n dp(i) = 1 + min(dp(i+1, remove), dp(i+1, replace), dp(i+1, insert))\n\n*/\n\nclass Solution {\npublic:\n int minDistance(string word1, string word2)...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "/*\n\ndp(i,j)\n\nw1[i] != w2[i]\n if i + 1 > len(w1)\n replace\n \n else\n if w[i+1] == w2[i]\n remove\n else\n dp(i) = 1 + min(dp(i+1, remove), dp(i+1, replace), dp(i+1, insert))\n\n*/\n\nclass Solution {\npublic:\n int minDistance(string word1, string word2)...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "/*\n\ndp(i,j)\n\nw1[i] != w2[i]\n if i + 1 > len(w1)\n replace\n \n else\n if w[i+1] == w2[i]\n remove\n else\n dp(i) = 1 + min(dp(i+1, remove), dp(i+1, replace), dp(i+1, insert))\n\n*/\n\nclass Solution {\npublic:\n int minDistance(string word1, string word2)...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "int getMin(vector<int> vals) {\n int min = vals[0];\n\n for (int i=1; i<vals.size(); i++) {\n if (vals[i] < min) {\n min = vals[i];\n }\n }\n\n return min;\n}\n\nclass Solution {\npublic:\n int minDistance(string word1, string word2) {\n int m = word1.size();\n int n = word2.siz...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int memo[501][501];\n int minOps(string word1, string word2, int i, int j){\n \n while(i<word1.size() && j<word2.size() && word1[i]==word2[j]){\n i++;\n j++;\n }\n\n if(i==word1.size()){\n return word2.size()-j;\...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int minDistance(const string& word1, const string& word2) {\n vector<int> cache(word1.size() * word2.size(), -1);\n\n const int s1 = word1.size();\n const int s2 = word2.size();\n\n const function<int(const string&, int, const string&, int)> distance = [&](auto w1, ...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\n\n int solve(string word1,string word2,int idx1,int idx2,vector<vector<int>>&dp){\n while(idx1 <word1.length() && idx2 < word2.length() && word1[idx1]==word2[idx2]){\n\n idx1++;\n idx2++;\n \n }\n //base case\n if(idx2==word2...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int dis(int i, int j, string word1, string word2, vector<vector<int>> &dp)\n {\n \n if(i==word1.size())\n return word2.size()-j;\n \n if(j==word2.size())\n return word1.size()-i;\n\n if(dp[i][j]!=-1)\n return dp[i][j]...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\n private:\n int f(int i,int j,string s,string t,vector<vector<int>>& dp)\n {\n if( j<0) return i+1; //i+1 deletiions\n if(i<0) return j+1; // j+1 insertions\n if(dp[i][j]!=-1) return dp[i][j];\n\n\n int replace=1+f(i-1,j-1,s,t,dp);\n\n if(s...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int fun(int index1,int index2, string word1, string word2,int n1,int n2,vector<vector<int>>&dp){\n if(index1==n1||index2==n2)\n return n1-index1+n2-index2;\n int operation=INT_MAX-1,noOperation=INT_MAX-1;\n if(dp[index1][index2]!=-1)\n ...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "using Cache = std::unordered_map<std::string, uint32_t>;\n\nusing PriorityMap = std::multimap<int32_t, std::pair<std::string, uint32_t>>;\n\n// Can return a number bigger, but not less.\nint32_t EstimatedDistance(const std::string_view s0,\n const std::string_view s1) {\n int32_...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "using Cache = std::unordered_map<std::string, uint32_t>;\n\nusing PriorityMap = std::multimap<int32_t, std::pair<std::string, uint32_t>>;\n\n// Can return a number bigger, but not less.\nint32_t EstimatedDistance(const std::string_view s0,\n const std::string_view s1) {\n int32_...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "using Cache = std::unordered_map<std::string, uint32_t>;\n\nusing PriorityMap = std::multimap<int32_t, std::pair<std::string, uint32_t>>;\n\n// Can return a number bigger, but not less.\nint32_t EstimatedDistance(const std::string_view s0,\n const std::string_view s1) {\n int32_...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "int dp[501][501];\nclass Solution {\npublic:\n int solve(string s, string t,int i,int j){\n if(i<0) return j+1;\n if(j<0) return i+1;\n if(dp[i][j]!=-1) return dp[i][j];\n\n if(s[i]==t[j]) \n return dp[i][j]=solve(s,t,i-1,j-1);\n else{\n int ins=1...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int dp[501][501];\n int min1(string word1, string word2,int i,int j)\n {\n if(i<0 && j>=0)\n\n {\n return j+1;\n }\n else if(j<0&& i>=0)\n return i+1;\n if(i<0 && j<0)\n return 0;\n if(dp[i][j]!=-1)\n ...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int dp[501][501];\n int solve(string s1,string s2,int i,int j){\n int n1=s1.length();\n int n2=s2.length();\n if(dp[i][j]!=-1)return dp[i][j];\n if(j>=n2)return (n1-i);\n if(i>=n1)return n2-j;\n\n\n if(s1[i]==s2[j]){\n r...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\nint dp[505][505];\nint helper(string word1, string word2, int i, int j){\n if(word1.size()==i) return word2.size()-j;\n if(word2.size()==j) return word1.size()-i;\n \n if(dp[i][j]!=-1) return dp[i][j]; \n if(word1[i]==word2[j]) return dp[i][j] = he...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\nint dp[505][505];\nint helper(string word1, string word2, int i, int j){\n if(word1.size()==i) return word2.size()-j;\n if(word2.size()==j) return word1.size()-i;\n \n if(dp[i][j]!=-1) return dp[i][j]; \n if(word1[i]==word2[j]) return dp[i][j] = he...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int dp[501][501];\n int solve(string word1,string word2,int i,int j){\n if(j>=word2.size()){\n return word1.size()-i;\n }\n if(i>=word1.size()){\n return word2.size()-j;\n }\n if(dp[i][j]!=-1)return dp[i][j];\n ...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic: \n\n int dp[501][501];\n int solve(string s1, string s2, int n, int m) {\n if (n == 0) return m;\n if (m == 0) return n;\n\n if (dp[n][m] != -1) return dp[n][m];\n\n if (s1[n-1] == s2[m-1]) {\n return dp[n][m] = solve(s1, s2, n-1, m-1)...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int t[501][501];\n int solve(int i,int j,string a,string b){\n if(i==a.size() || j==b.size()) {\n if(i==a.size()) return b.size()-j+1;\n else return a.size()-i+1;\n }\n\n if(t[i][j]!=-1) return t[i][j];\n\n int skip=INT_MAX...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int dp[501][501];\n int func(int ind1,int ind2,string word1,string word2){\n // if((ind1==0 && ind2==0) && word1[ind1]!=word2[ind2]) return 1;\n if(ind1<0) return ind2+1;\n if(ind2<0) return ind1+1;\n if(dp[ind1][ind2]!=-1) return dp[ind1][ind2]...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int t[501][501];\n int solve(string w1, string w2, int m, int n){\n if(m==0)\n return n;\n if(n==0)\n return m;\n if(t[m][n]!=-1)\n return t[m][n];\n if(w1[m-1]==w2[n-1])\n return t[m][n]=solve(w1,w2,m...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int dp[501][501];\n int minDistanceHelper(string word1, int i, string word2, int j) {\n if(i==word1.size() || j==word2.size()) {\n return max(word2.size()-j, word1.size()-i);\n }\n if(dp[i][j]!=-1)\n return dp[i][j];\n if(w...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int n,m;\n int dp[510][510];\n int rec(int i,int j,string word1, string word2){\n //base case\n // if(i<0 || j<0){\n // return 1+max(i,j);\n // }\n\n if(i==n) return m-1-j+1;\n if(j==m) return n-1-i+1;\n\n //caching\n...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\n int dp[501][501];\n int count(string word1, string word2, int n, int m) {\n if (n == 0) return m;\n if (m == 0) return n;\n if (dp[n][m] != -1) return dp[n][m];\n if (word1[n-1] == word2[m-1]) {\n dp[n][m] = count(word1, word2, n-1, m-1);\n ...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\n int dp[501][501];\n int count(string word1, string word2, int n, int m) {\n if (n == 0) return m;\n if (m == 0) return n;\n if (dp[n][m] != -1) return dp[n][m];\n if (word1[n-1] == word2[m-1]) {\n dp[n][m] = count(word1, word2, n-1, m-1);\n ...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int dp[501][501];\n int yu(int i,int j,string w1, string w2){\n if(i>=w1.size()){\n return w2.size()-j;\n }\n if(j>=w2.size()){\n return w1.size()-i;\n }\n if(dp[i][j]!=-1){\n return dp[i][j];\n }\n...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int f(int i,int j,string word1,string word2,vector<vector<int>>&dp)\n {\n if(i<0)\n {\n return j+1;\n }\n if(j<0)\n {\n return i+1;\n }\n if(dp[i][j]!=-1)\n {\n return dp[i][j];\n ...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int solve(int i, int j, string word1, string word2, vector<vector<int>>& dp){\n if(i < 0){\n return j + 1;\n }\n if(j < 0){\n return i + 1;\n }\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n if(...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\nint solve(string word1,string word2,int index1,int index2,vector<vector<int>>&dp)\n{\n if(index1==word1.length())\n {\n return word2.length()-index2;\n }\n if(index2==word2.length())\n {\n return word1.length()-index1;\n }\n if(dp[index1][index2...
72
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
3
{ "code": "class Solution {\npublic:\n int solve(string a,string b,int i,int j,vector<vector<int>>&dp){\n if(i==a.length()){\n return b.length()-j;\n }\n\n if(j==b.length()){\n return a.length()-i;\n }\n if(dp[i][j]!=-1){\n return dp[i][j];\n ...
73
<p>Given an <code>m x n</code> integer matrix <code>matrix</code>, if an element is <code>0</code>, set its entire row and column to <code>0</code>&#39;s.</p> <p>You must do it <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in place</a>.</p> <p>&nbsp;</p> <p><strong class="example">Example...
0
{ "code": "class Solution {\npublic:\n void setZeroes(vector<vector<int>>& matrix) {\n vector<pair<int,int>>index;\n\n for(int i=0;i<matrix.size();i++){\n for(int j=0;j<matrix[0].size();j++){\n if(matrix[i][j] == 0){\n pair<int,int>p = make_pair(i,j);\n ...
73
<p>Given an <code>m x n</code> integer matrix <code>matrix</code>, if an element is <code>0</code>, set its entire row and column to <code>0</code>&#39;s.</p> <p>You must do it <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in place</a>.</p> <p>&nbsp;</p> <p><strong class="example">Example...
0
{ "code": "class Solution {\npublic:\n // O(n * m) time | O(1) space\n void setZeroes(vector<vector<int>>& matrix) {\n int rows = size(matrix), cols = size(matrix[0]);\n bool sweepFirstRow = false, sweepFirstCol = false;\n\n // markers\n for (int r = 0; r < rows; ++r) {\n ...
73
<p>Given an <code>m x n</code> integer matrix <code>matrix</code>, if an element is <code>0</code>, set its entire row and column to <code>0</code>&#39;s.</p> <p>You must do it <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in place</a>.</p> <p>&nbsp;</p> <p><strong class="example">Example...
0
{ "code": "class Solution {\npublic:\n void setZeroes(vector<vector<int>>& matrix) {\n int n = matrix.size(); // Number of rows in the matrix\n int m = matrix[0].size(); // Number of columns in the matrix\n bool flag1 = false; // Flag to check if the first column contains ...
73
<p>Given an <code>m x n</code> integer matrix <code>matrix</code>, if an element is <code>0</code>, set its entire row and column to <code>0</code>&#39;s.</p> <p>You must do it <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in place</a>.</p> <p>&nbsp;</p> <p><strong class="example">Example...
0
{ "code": "class Solution {\npublic:\n void setZeroes(vector<vector<int>>& m) {\n\n // a better approach is to store the index of those rows and columns which going to be zero and then run a loop to make them 0;\n\n // a optimize approach then the previous better approach is that instead of creating ...
73
<p>Given an <code>m x n</code> integer matrix <code>matrix</code>, if an element is <code>0</code>, set its entire row and column to <code>0</code>&#39;s.</p> <p>You must do it <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in place</a>.</p> <p>&nbsp;</p> <p><strong class="example">Example...
0
{ "code": "class Solution {\npublic:\n void setZeroes(vector<vector<int>>& matrix) {\n int m = matrix.size();\n int n = matrix[0].size();\n \n bool firstRowZero = false, firstColZero = false;\n \n // Check if the first row has any zeros\n for (int j = 0; j < n; j++)...
73
<p>Given an <code>m x n</code> integer matrix <code>matrix</code>, if an element is <code>0</code>, set its entire row and column to <code>0</code>&#39;s.</p> <p>You must do it <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in place</a>.</p> <p>&nbsp;</p> <p><strong class="example">Example...
0
{ "code": "class Solution {\npublic:\n void setZeroes(vector<vector<int>>& matrix) {\n //int col[m] = {0} -> matrix[0][..] -> row[0]\n //int row[n] = {0} -> matrix[..][0] -> col[0]\n int n = matrix.size();\n int m = matrix[0].size();\n int col0 = 1;\n for(int i=0;i<n;i++){...
73
<p>Given an <code>m x n</code> integer matrix <code>matrix</code>, if an element is <code>0</code>, set its entire row and column to <code>0</code>&#39;s.</p> <p>You must do it <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in place</a>.</p> <p>&nbsp;</p> <p><strong class="example">Example...
0
{ "code": "class Solution {\npublic:\n void setZeroes(vector<vector<int>>& matrix) {\n int m = matrix.size();\n int n = matrix[0].size();\n vector<int> row(m, 0), column(n, 0);\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (matrix[i][j] =...
73
<p>Given an <code>m x n</code> integer matrix <code>matrix</code>, if an element is <code>0</code>, set its entire row and column to <code>0</code>&#39;s.</p> <p>You must do it <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in place</a>.</p> <p>&nbsp;</p> <p><strong class="example">Example...
0
{ "code": "class Solution {\npublic:\n void setZeroes(vector<vector<int>>& matrix) {\n vector<pair<int , int>>temp;\n int cnt=0;\n int matrows=matrix.size();\n int matcols=matrix[0].size();\n for(int i=0;i<matrix.size();i++){\n for(int j=0;j<matrix[0].size();...
73
<p>Given an <code>m x n</code> integer matrix <code>matrix</code>, if an element is <code>0</code>, set its entire row and column to <code>0</code>&#39;s.</p> <p>You must do it <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in place</a>.</p> <p>&nbsp;</p> <p><strong class="example">Example...
1
{ "code": "class Solution {\npublic:\n void setZeroes(vector<vector<int>>& matrix) {\n int n = matrix.size() ; \n int m = matrix[0].size() ; \n for(int i = 0 ; i< n ; i++){\n for(int j = 0 ; j<m ; j++){\n if(matrix[i][j]==0){\n matrix[i][j] = -8612 ...
73
<p>Given an <code>m x n</code> integer matrix <code>matrix</code>, if an element is <code>0</code>, set its entire row and column to <code>0</code>&#39;s.</p> <p>You must do it <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in place</a>.</p> <p>&nbsp;</p> <p><strong class="example">Example...
3
{ "code": "class Solution {\npublic:\n void setZeroes(vector<vector<int>>& matrix) {\n int m=matrix.size();\n int n = matrix[0].size();\n set<int>dummy_row;\n set<int>dummy_col;\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n if(matrix[i][j]==0){\n ...
73
<p>Given an <code>m x n</code> integer matrix <code>matrix</code>, if an element is <code>0</code>, set its entire row and column to <code>0</code>&#39;s.</p> <p>You must do it <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in place</a>.</p> <p>&nbsp;</p> <p><strong class="example">Example...
3
{ "code": "class Solution {\npublic:\n void setZeroes(vector<vector<int>>& matrix) {\n int n = matrix.size();\n int m = matrix[0].size();\n vector<vector<int>> visited = matrix;\n for(int i=0; i<n; i++){\n for(int j=0; j<m; j++){\n if(matrix[i][j] == 0){\n ...
74
<p>You are given an <code>m x n</code> integer matrix <code>matrix</code> with the following two properties:</p> <ul> <li>Each row is sorted in non-decreasing order.</li> <li>The first integer of each row is greater than the last integer of the previous row.</li> </ul> <p>Given an integer <code>target</code>, retur...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target) {\n int m=matrix.size();\n int n=matrix[0].size();\n int t=0;\n int b=m-1;\n int mid;\n while(t<=b){\n mid=(t+b)/2;\n if(matrix[mid][0]==target){\n ...
74
<p>You are given an <code>m x n</code> integer matrix <code>matrix</code> with the following two properties:</p> <ul> <li>Each row is sorted in non-decreasing order.</li> <li>The first integer of each row is greater than the last integer of the previous row.</li> </ul> <p>Given an integer <code>target</code>, retur...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target){\n int row=matrix.size();\n int column=matrix[0].size();\n int s=0;\n int e=(row*column)-1;\n int mid=s+(e-s)/2;\n while(s<=e)\n {\n int element=matrix[mid/column][mid%...
74
<p>You are given an <code>m x n</code> integer matrix <code>matrix</code> with the following two properties:</p> <ul> <li>Each row is sorted in non-decreasing order.</li> <li>The first integer of each row is greater than the last integer of the previous row.</li> </ul> <p>Given an integer <code>target</code>, retur...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target) {\n int m = matrix.size(); // row count\n int n = matrix[0].size(); // column count\n int l = 0; int r = (m*n) - 1;\n\n while (l <= r) {\n int M = l + (r-l) / 2;\n ...
74
<p>You are given an <code>m x n</code> integer matrix <code>matrix</code> with the following two properties:</p> <ul> <li>Each row is sorted in non-decreasing order.</li> <li>The first integer of each row is greater than the last integer of the previous row.</li> </ul> <p>Given an integer <code>target</code>, retur...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target) {\n int rows = matrix.size();\n int cols = matrix[0].size();\n int i = 0;\n int j = cols - 1;\n\n // while(i<=rows && j >=0){\n // if(matrix[i][j] == target) return true;...
74
<p>You are given an <code>m x n</code> integer matrix <code>matrix</code> with the following two properties:</p> <ul> <li>Each row is sorted in non-decreasing order.</li> <li>The first integer of each row is greater than the last integer of the previous row.</li> </ul> <p>Given an integer <code>target</code>, retur...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target) {\n if(matrix.empty() || matrix[0].empty()) return false;\n\n int row=matrix.size();\n int col=matrix[0].size();\n\n int i=0,j=col-1;\n\n while(i<row && j>=0){\n if(matrix[i][j] ==...
74
<p>You are given an <code>m x n</code> integer matrix <code>matrix</code> with the following two properties:</p> <ul> <li>Each row is sorted in non-decreasing order.</li> <li>The first integer of each row is greater than the last integer of the previous row.</li> </ul> <p>Given an integer <code>target</code>, retur...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target) {\n int rows = matrix.size(),\n\t\t\tcols = matrix[0].size(),\n row = 0, col = cols - 1;\n\t\t\t\n while (row < rows && col > -1) {\n int cur = matrix[row][col];\n if (cu...
74
<p>You are given an <code>m x n</code> integer matrix <code>matrix</code> with the following two properties:</p> <ul> <li>Each row is sorted in non-decreasing order.</li> <li>The first integer of each row is greater than the last integer of the previous row.</li> </ul> <p>Given an integer <code>target</code>, retur...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target) {\n \n int row = matrix.size();\n int col = matrix[0].size();\n\n int low = 0;\n int high = row * col - 1;\n\n while(low <= high){\n\n int mid = low + (high - low) ...
74
<p>You are given an <code>m x n</code> integer matrix <code>matrix</code> with the following two properties:</p> <ul> <li>Each row is sorted in non-decreasing order.</li> <li>The first integer of each row is greater than the last integer of the previous row.</li> </ul> <p>Given an integer <code>target</code>, retur...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target) {\n int r=matrix.size(),c=matrix[0].size();\n int s=0,l=r-1,mid,ans=-1;\n while(s<=l){\n mid=(s+l)/2;\n if(target==matrix[mid][0]){\n return true;\n ...
74
<p>You are given an <code>m x n</code> integer matrix <code>matrix</code> with the following two properties:</p> <ul> <li>Each row is sorted in non-decreasing order.</li> <li>The first integer of each row is greater than the last integer of the previous row.</li> </ul> <p>Given an integer <code>target</code>, retur...
0
{ "code": "class Solution {\npublic:\n bool searchMatrix(vector<vector<int>>& matrix, int target) {\n int n=matrix.size();\n int m=matrix[0].size();\n\n\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(matrix[i][j]==target){\n return true;\n ...
75
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
0
{ "code": "#include <iostream>\n#include <fstream>\n#include <vector>\n#include <string>\n#include <algorithm>\n\n#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n...
75
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
0
{ "code": "#include <iostream>\n#include <fstream>\n#include <vector>\n#include <string>\n#include <algorithm>\n\n#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n...
75
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
0
{ "code": "#include <iostream>\n#include <fstream>\n#include <vector>\n#include <string>\n#include <algorithm>\n\n#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n...
75
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
0
{ "code": "#include <iostream>\n#include <fstream>\n#include <vector>\n#include <string>\n#include <algorithm>\n\n#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n...
75
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
0
{ "code": "class Solution {\npublic:\n void sortColors(vector<int>& nums) {\n int i = 0; \n int j = 0; \n int k = nums.size() - 1;\n while(j<=k)\n {\n if(nums[j] == 2)\n {\n swap(nums[j], nums[k]);\n k--;\n } \n ...
75
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
0
{ "code": "class Solution {\npublic:\n void sortColors(vector<int>& nums) {\n int l = 0,h=nums.size()-1,m=0;\n while(m<=h) {\n if(nums[m]==0) {\n int temp = nums[l];\n nums[l] = nums[m];\n nums[m] = temp;\n l++;\n m+...
75
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
0
{ "code": "class Solution {\npublic:\n void sortColors(vector<int>& nums) {\n int z = 0;\n int o = 0;\n int t = 0;\n for(int i =0;i<nums.size();i++){\n if ( nums[i] == 0 ) z++;\n else if (nums[i]== 1) o++;\n else t++;\n }\n for(int i =0;i<n...
75
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
0
{ "code": "class Solution {\npublic:\n void sortColors(vector<int>& nums) {\n int start = 0, mid = 0, end = nums.size() - 1;\n while (mid <= end) {\n switch (nums[mid]) {\n case 0: {\n swap(nums[start++], nums[mid++]);\n break;\n }\n ...
75
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
0
{ "code": "class Solution {\npublic:\n void sortColors(vector<int>& nums) {\n \n int low=0;\n int mid=0;\n int high=nums.size()-1;\n\n while(mid<=high){\n if(nums[mid]==1){\n mid++;\n }\n\n else if(nums[mid]==2){\n sw...
75
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
0
{ "code": "class Solution {\npublic:\n void sortColors(vector<int>& nums) {\n int i=0,j=0;\n int n=nums.size()-1;\n while(j<=n){\n if(nums[j]==1) j++;\n else if(nums[j]==0){\n swap(nums[i],nums[j]);\n j++;i++;\n }else if(nums[j]==2...
75
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
0
{ "code": "class Solution {\npublic:\n void sortColors(vector<int>& nums) {\n int zero, one, two;\n zero = one = two = 0;\n for(auto& num: nums){\n if(num == 0) ++zero;\n else if(num == 1) ++one;\n else ++two;\n }\n int n = nums.size();\n f...
75
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
0
{ "code": "class Solution {\npublic:\n void sortColors(vector<int>& nums) {\n int low=0, mid=0, high=nums.size()-1, n=nums.size();\n while(mid<=high) {\n if(nums[mid]==1) mid++;\n else if(nums[mid]==2) {\n nums[mid]=nums[high];\n nums[high]=2;\n ...
75
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
1
{ "code": "class Solution {\npublic:\n void sortColors(vector<int>& nums) {\n for(int i=0;i<nums.size()-1;i++){\n for(int j=i+1;j<nums.size();j++){\n if(nums[i]>nums[j]){\n swap(nums[i],nums[j]);\n }\n }\n }\n }\n};\n", "memo...
75
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
1
{ "code": "class Solution {\npublic:\n void sortColors(vector<int>& nums) {\n int cZero = 0, cOne = 0, cTwo = 0;\n\n int n = nums.size();\n for(int i = 0; i < n; i++) {\n if(nums[i] == 0) {\n cZero++;\n }\n else if(nums[i] == 1) {\n ...
75
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
2
{ "code": "class Solution {\npublic:\n void sortColors(vector<int>& nums) {\n int n=nums.size();\n int c0 = 0, c1 = 0, c2 = 0;\n\n for(int i:nums){\n if(i==0){\n c0++;\n }\n else if(i==1){\n c1++;\n }\n e...
75
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
2
{ "code": "class Solution {\npublic:\n void sortColors(vector<int>& nums)\n {\n sort(nums.begin(),nums.end());\n \n }\n};", "memory": "10300" }
76
<p>Given two strings <code>s</code> and <code>t</code> of lengths <code>m</code> and <code>n</code> respectively, return <em>the <strong>minimum window</strong></em> <span data-keyword="substring-nonempty"><strong><em>substring</em></strong></span><em> of </em><code>s</code><em> such that every character in </em><code>...
0
{ "code": "class Solution {\npublic:\n static string minWindow(const string &str, const string &pattern) {\n array<int, 58> required{};\n for (const char letter : pattern) {\n ++required[letter - 'A'];\n }\n\n const auto not_enough = [&]() -> bool {\n for (array<int, 58>::size_type i = 0; i < 5...
76
<p>Given two strings <code>s</code> and <code>t</code> of lengths <code>m</code> and <code>n</code> respectively, return <em>the <strong>minimum window</strong></em> <span data-keyword="substring-nonempty"><strong><em>substring</em></strong></span><em> of </em><code>s</code><em> such that every character in </em><code>...
0
{ "code": "class Solution {\npublic:\n string minWindow(string& s, string& t) {\n if (s.size() < t.size()) return \"\";\n short counts[58] = {0};\n for (char ch : t) counts[ch-'A']++;\n short mismatches = 0;\n for (int c : counts) if (c != 0) mismatches++;\n int i = 0, j =...
76
<p>Given two strings <code>s</code> and <code>t</code> of lengths <code>m</code> and <code>n</code> respectively, return <em>the <strong>minimum window</strong></em> <span data-keyword="substring-nonempty"><strong><em>substring</em></strong></span><em> of </em><code>s</code><em> such that every character in </em><code>...
0
{ "code": "int counter[1 << 8];\n\nclass Solution {\npublic:\n string minWindow(string s, string t) {\n for (int i = 0; i < 1 << 8; i++)\n counter[i] = 0;\n for (char c : t)\n counter[c]++;\n\n int n = s.size();\n int i = 0, j = 0, tChars = 0;\n int start = ...
76
<p>Given two strings <code>s</code> and <code>t</code> of lengths <code>m</code> and <code>n</code> respectively, return <em>the <strong>minimum window</strong></em> <span data-keyword="substring-nonempty"><strong><em>substring</em></strong></span><em> of </em><code>s</code><em> such that every character in </em><code>...
0
{ "code": "// strings \n// s - length m - bigger string\n// t - length n\n// Return minimum window substring of s (return the substring) such that \n// every char in t (including duplicates) is included. \n// - Does order matter?\n// \n// Example:\n// s - ADOBECODEBANC\n// t - ABC \n// \n// Hashtable to...
76
<p>Given two strings <code>s</code> and <code>t</code> of lengths <code>m</code> and <code>n</code> respectively, return <em>the <strong>minimum window</strong></em> <span data-keyword="substring-nonempty"><strong><em>substring</em></strong></span><em> of </em><code>s</code><em> such that every character in </em><code>...
0
{ "code": "class Solution {\npublic:\n string minWindow(string s, string t) {\n vector<int> freqT(128);\n for (char ch : t) {\n freqT[ch]++;\n }\n\n int counter = 0;\n\n for (int i = 0; i < 128; i++) {\n if(freqT[i] > 0) counter++;\n }\n\n int ...
76
<p>Given two strings <code>s</code> and <code>t</code> of lengths <code>m</code> and <code>n</code> respectively, return <em>the <strong>minimum window</strong></em> <span data-keyword="substring-nonempty"><strong><em>substring</em></strong></span><em> of </em><code>s</code><em> such that every character in </em><code>...
0
{ "code": "class Solution {\npublic:\n string minWindow(string s, string t) {\n vector<int> hash(60, 0);\n for(int i = 0; i < t.size(); i++) hash[t[i]-'A']++;\n int n = s.length(), m = t.length(), l = 0, r = 0, len = INT_MAX, cnt = 0;\n int start = -1;\n\n while(r < n) {\n ...
76
<p>Given two strings <code>s</code> and <code>t</code> of lengths <code>m</code> and <code>n</code> respectively, return <em>the <strong>minimum window</strong></em> <span data-keyword="substring-nonempty"><strong><em>substring</em></strong></span><em> of </em><code>s</code><em> such that every character in </em><code>...
0
{ "code": "class Solution {\n public:\n string minWindow(string s, string t) {\n vector<int> count(128);\n int required = t.length();\n int bestLeft = -1;\n int minLength = s.length() + 1;\n\n for (const char c : t)\n ++count[c];\n\n for (int l = 0, r = 0; r < s.length(); ++r) {\n if (--c...
76
<p>Given two strings <code>s</code> and <code>t</code> of lengths <code>m</code> and <code>n</code> respectively, return <em>the <strong>minimum window</strong></em> <span data-keyword="substring-nonempty"><strong><em>substring</em></strong></span><em> of </em><code>s</code><em> such that every character in </em><code>...
0
{ "code": "class Solution {\npublic:\n string minWindow(string s, string t) {\n vector<int> map(128,0);\n for(auto c: t) map[c]++;\n int counter=t.size(), begin=0, end=0, d=INT_MAX, head=0;\n while(end<s.size()){\n if(map[s[end++]]-->0) counter--; //in t\n while(co...
76
<p>Given two strings <code>s</code> and <code>t</code> of lengths <code>m</code> and <code>n</code> respectively, return <em>the <strong>minimum window</strong></em> <span data-keyword="substring-nonempty"><strong><em>substring</em></strong></span><em> of </em><code>s</code><em> such that every character in </em><code>...
0
{ "code": "class Solution {\n static constexpr char index(char c) {\n return c - (c >= 'a' ? 'a' - 26 : 'A');\n }\n\npublic:\n string minWindow(string s, string t) {\n int count = t.length();\n std::array<int, 2*26> chars = {};\n\n for (auto c: t)\n chars[index(c)]++;\n...
76
<p>Given two strings <code>s</code> and <code>t</code> of lengths <code>m</code> and <code>n</code> respectively, return <em>the <strong>minimum window</strong></em> <span data-keyword="substring-nonempty"><strong><em>substring</em></strong></span><em> of </em><code>s</code><em> such that every character in </em><code>...
0
{ "code": "class Solution {\npublic:\n bool contain(vector<int>& v){\n for(int i=0; i<v.size();i++){\n if(v[i]<0) return false;\n }\n return true;\n }\n string minWindow(string s, string t) {\n vector<int> v('z'-'A'+8,0);\n for(int i=0;i<t.size();i++){\n ...
76
<p>Given two strings <code>s</code> and <code>t</code> of lengths <code>m</code> and <code>n</code> respectively, return <em>the <strong>minimum window</strong></em> <span data-keyword="substring-nonempty"><strong><em>substring</em></strong></span><em> of </em><code>s</code><em> such that every character in </em><code>...
0
{ "code": "class Solution {\n public:\n string minWindow(string s, string t) {\n vector<int> count(128);\n int required = t.length();\n int bestLeft = -1;\n int minLength = s.length() + 1;\n\n for (const char c : t)\n ++count[c];\n\n for (int l = 0, r = 0; r < s.length(); ++r) {\n if (--c...
76
<p>Given two strings <code>s</code> and <code>t</code> of lengths <code>m</code> and <code>n</code> respectively, return <em>the <strong>minimum window</strong></em> <span data-keyword="substring-nonempty"><strong><em>substring</em></strong></span><em> of </em><code>s</code><em> such that every character in </em><code>...
0
{ "code": "class Solution {\npublic:\n string minWindow(string s, string t) {\n vector<int> buckets = vector<int>(256);\n int numUniques = 0;\n \n for(char c : t)\n {\n if(buckets[c] == 0)\n numUniques++;\n \n buckets[c]++;\n ...