title
stringlengths
1
100
titleSlug
stringlengths
3
77
Java
int64
0
1
Python3
int64
1
1
content
stringlengths
28
44.4k
voteCount
int64
0
3.67k
question_content
stringlengths
65
5k
question_hints
stringclasses
970 values
Image Explanation🏆- [Simple, Easy & Concise - Stack] - C++/Java/Python
simplify-path
1
1
# Video Solution (`Aryan Mittal`) - Link in LeetCode Profile\n`Simplify Path` by `Aryan Mittal`\n![lc.png](https://assets.leetcode.com/users/images/0dcce590-ae89-40a6-81b8-727f896098a7_1681264846.7869709.png)\n\n\n# Approach & Intution\n![image.png](https://assets.leetcode.com/users/images/6c0be3d7-dea4-44ec-a4a8-2dcca...
74
Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**. In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level,...
null
Python faster than 99.8% super simple solution
simplify-path
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThink of it as a filesytem path and split it into folders, forget the "/"\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e....
0
Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**. In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level,...
null
🐍 python solution using stack; O(N) faster than 99.92%
simplify-path
0
1
# Complexity\n- Time complexity: O(N)\n\n- Space complexity: O(N)\n\n# Code\n```\nclass Solution:\n def simplifyPath(self, path: str) -> str:\n new_path = []\n for folder in path.split(\'/\'):\n if folder!=\'\' and folder!=\'.\' and folder!=\'..\':\n new_path.append(folder) \n...
1
Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**. In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level,...
null
✅✅Python🔥Java 🔥C++🔥Simple Solution🔥🔥Easy to Understand🔥🔥
simplify-path
1
1
# Please UPVOTE \uD83D\uDC4D\n\n**!! BIG ANNOUNCEMENT !!**\nI am Giving away my premium content videos related to computer science and data science and also will be sharing well-structured assignments and study materials to clear interviews at top companies to my first 10,000 Subscribers. So, **DON\'T FORGET** to Subsc...
45
Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**. In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level,...
null
Python- More example Testcases + Easily explained solution
simplify-path
0
1
# Example test cases\nSimplify the directory path (Unix like)\nGiven an absolute path for a file (Unix-style), simplify it. Note that absolute path always begin with \u2018/\u2019 ( root directory ), a dot in path represent current directory and double dot represents parent directory.\n\nExamples:\n\n"/a/./" --> means ...
1
Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**. In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level,...
null
Easy solution Python | using split :D
simplify-path
0
1
# Code\n```\nclass Solution:\n def simplifyPath(self, path: str) -> str:\n cadenas = path.split(\'/\')\n arr = []\n for s in cadenas:\n if s != \'.\' and s != \'..\' and s:\n arr.append(s)\n elif s == "..":\n if arr:\n arr.po...
1
Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**. In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level,...
null
One line solution
simplify-path
0
1
# Code\n```\nclass Solution:\n def simplifyPath(self, path: str) -> str:\n return __import__(\'os\').path.abspath(path)\n```
5
Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**. In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level,...
null
Line by line explanation | Beats 100% Time and space, [Python],
simplify-path
0
1
Here is Line by line code in \n**Python :**\n```\ndef simplify_path(path):\n # Split path into a list of directory names\n dirs = path.split(\'/\')\n # Initialize the stack of directories\n stack = []\n # Iterate through the directories\n for d in dirs:\n # Ignore double slashes\n if d =...
10
Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**. In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level,...
null
Python Simple solution using Stack
simplify-path
0
1
\n# Code\n```\nclass Solution:\n def simplifyPath(self, path: str) -> str:\n stack = []\n\n # different directories present in the string\n temp = path.split(\'/\') \n\n for i in temp:\n if i != \'.\' and i != \'\' and i != \'..\':\n stack.append(i) # add if it i...
5
Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**. In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level,...
null
Simple python solution
simplify-path
0
1
# Complexity\n- Time complexity: Given `n = len(path)` then complexity is `O(n)`\n\n- Space complexity: Given `n = len(path)` then complexity is `O(n)`\n\n# Code\n```\nclass Solution:\n def simplifyPath(self, path: str) -> str:\n levels, stack = path.split("/"), []\n\n for l in levels:\n if ...
2
Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**. In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level,...
null
Simple Python Solution using stack
simplify-path
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n```\nStep 1 : spliting with respect to "/"\nStep 2 : if ".." (parent directory) so pop the current dirctory ie top one in the stack\n if "." (current directory) so dont add to stack\n if null then dont add\n if n...
1
Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**. In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level,...
null
python3 solution
simplify-path
0
1
\n```\nclass Solution:\n def simplifyPath(self, path: str) -> str:\n \n stack=[]\n for a in path.split(\'/\'):\n if a==\'..\':\n if stack:\n stack.pop()\n\n elif a not in (\'\',\'.\'):\n stack.append(a)\n\n\n return "/"+"/...
4
Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**. In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level,...
null
Easy Python Solution Using Stacks | Easy to Understand
simplify-path
0
1
\n# Complexity\n- Time complexity: $$ O(n) $$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$ O(n) $$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def simplifyPath(self, path: str) -> str:\n paths = path.split(\'/\')\n st = [...
3
Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**. In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level,...
null
Clean Codes🔥🔥|| Full Explanation✅|| Dynamic Programming✅|| C++|| Java|| Python3
edit-distance
1
1
# Intuition :\n- Here we have to find the minimum edit distance problem between two strings word1 and word2. \n- The minimum edit distance is defined as the minimum number of operations required to transform one string into another.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach :\n-...
322
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_. You have the following three operations permitted on a word: * Insert a character * Delete a character * Replace a character **Example 1:** **Input:** word1 = "horse ", word2 = "ros " **O...
null
Bottom up DP
edit-distance
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_. You have the following three operations permitted on a word: * Insert a character * Delete a character * Replace a character **Example 1:** **Input:** word1 = "horse ", word2 = "ros " **O...
null
Python easy to understand | Dynamic Programming | Tabulation (Bottom-Up Approach)
edit-distance
0
1
# Complexity\n- Time complexity: $$O(m*n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(m*n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def minDistance(self, word1: str, word2: str) -> int:\n m, n = len(word1), len(word2)\n...
1
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_. You have the following three operations permitted on a word: * Insert a character * Delete a character * Replace a character **Example 1:** **Input:** word1 = "horse ", word2 = "ros " **O...
null
Simple DP Solution with Comments
edit-distance
0
1
# Code\n```\nclass Solution:\n def fun(self, word1, word2, dp):\n\n # check if the result is already present in the dp table\n if dp[len(word2)][len(word1)] != -1:\n return dp[len(word2)][len(word1)] \n\n # if both words are empty, then the number of operations required is 0\...
1
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_. You have the following three operations permitted on a word: * Insert a character * Delete a character * Replace a character **Example 1:** **Input:** word1 = "horse ", word2 = "ros " **O...
null
DP || Tabulation || Commented line by line
edit-distance
0
1
\n\n# Code\n```\nclass Solution:\n def minDistance(self, word1: str, word2: str) -> int:\n #In tabulation method, we\'ll assign rows and columns\n #wih length of given words\n len1=len(word1)+1\n len2=len(word2)+1\n dp=[[-1 for j in range(len2)] for j in range(len1)]\n #if i...
1
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_. You have the following three operations permitted on a word: * Insert a character * Delete a character * Replace a character **Example 1:** **Input:** word1 = "horse ", word2 = "ros " **O...
null
Solution
edit-distance
1
1
```C++ []\nclass Solution {\npublic:\n vector<vector<int>> dp;\n \n int solve(int i, int j, string &s, string &t){\n if(i<0 && j<0)\n return 0;\n \n if(i < 0 && j>=0){\n return j+1;\n }\n \n if(i>=0 && j<0){\n return i+1;\n }\n ...
26
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_. You have the following three operations permitted on a word: * Insert a character * Delete a character * Replace a character **Example 1:** **Input:** word1 = "horse ", word2 = "ros " **O...
null
🥳【Python3】🔥 Easy Solution
edit-distance
0
1
**Python3 Solution**\n```python\nclass Solution:\n def minDistance(self, word1: str, word2: str) -> int:\n m, n = len(word1), len(word2)\n dp = [[0] * (n + 1) for _ in range(m + 1)]\n # base case - i steps away\n for i in range(1, m + 1):\n dp[i][0] = i\n for j in range(...
8
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_. You have the following three operations permitted on a word: * Insert a character * Delete a character * Replace a character **Example 1:** **Input:** word1 = "horse ", word2 = "ros " **O...
null
Very Simple Dp solution
edit-distance
0
1
\n# Complexity\n- Time complexity: O(n*m)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n*m)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def minDistance(self, word1: str, word2: str) -> int:\n n, m = len(word1), len(word2)\n ...
1
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_. You have the following three operations permitted on a word: * Insert a character * Delete a character * Replace a character **Example 1:** **Input:** word1 = "horse ", word2 = "ros " **O...
null
100 % SPACE OPTIMIZED SOLUTION || DP
edit-distance
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:$$O(N*M)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(M)$$\n<!-- Add your space complexity here...
2
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_. You have the following three operations permitted on a word: * Insert a character * Delete a character * Replace a character **Example 1:** **Input:** word1 = "horse ", word2 = "ros " **O...
null
Python || 91.89% Faster || DP || Memoization+Tabulation
edit-distance
0
1
```\n\'\'\'\nExample: s1: \'horse\'\n s2: \'ros\'\n\nCase1: Inserting a Character\n\nNow if we have to match the strings by insertions, what would we do?: \n\nWe would have placed an \u2018s\u2019 at index 5 of S1.\nSuppose i now point to s at index 5 of S1 and j points are already pointing to s at index j of S...
3
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_. You have the following three operations permitted on a word: * Insert a character * Delete a character * Replace a character **Example 1:** **Input:** word1 = "horse ", word2 = "ros " **O...
null
Python 3 || 8 lines, w/ comments || T/M: 100% / 78%
edit-distance
0
1
```\nclass Solution:\n def minDistance(self, word1: str, word2: str) -> int:\n\n w1, w2 = len(word1), len(word2)\n \n @lru_cache(None)\n def dp(i, j):\n\n if i >= w1 : return w2-j # word1 used up, so all inserts\n if j >= w2 : r...
18
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_. You have the following three operations permitted on a word: * Insert a character * Delete a character * Replace a character **Example 1:** **Input:** word1 = "horse ", word2 = "ros " **O...
null
Helper Function (Beats 90% and 99%)
set-matrix-zeroes
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. ...
Helper Function (Beats 90% and 99%)
set-matrix-zeroes
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. W...
Python Solution w/ approach explanation & readable with space progression from: O(m+n) & O(1)
set-matrix-zeroes
0
1
Note: m = number of rows, n = number of cols\n\n**Brute force using O(m*n) space:** The initial approach is to start with creating another matrix to store the result. From doing that, you\'ll notice that we want a way to know when each row and col should be changed to zero. We don\'t want to prematurely change the valu...
195
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. ...
Python Solution w/ approach explanation & readable with space progression from: O(m+n) & O(1)
set-matrix-zeroes
0
1
Note: m = number of rows, n = number of cols\n\n**Brute force using O(m*n) space:** The initial approach is to start with creating another matrix to store the result. From doing that, you\'ll notice that we want a way to know when each row and col should be changed to zero. We don\'t want to prematurely change the valu...
195
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. W...
Two Different Approaches
set-matrix-zeroes
0
1
# without Space ---->O(1)\n# Time complexity ------>O(N^3)\n```\nclass Solution:\n def setZeroes(self, matrix: List[List[int]]) -> None:\n m=len(matrix)\n n=len(matrix[0])\n for i in range(m):\n for j in range(n):\n if matrix[i][j]==0:\n for row in ra...
13
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. ...
Two Different Approaches
set-matrix-zeroes
0
1
# without Space ---->O(1)\n# Time complexity ------>O(N^3)\n```\nclass Solution:\n def setZeroes(self, matrix: List[List[int]]) -> None:\n m=len(matrix)\n n=len(matrix[0])\n for i in range(m):\n for j in range(n):\n if matrix[i][j]==0:\n for row in ra...
13
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. W...
🔥[Python3] O(1) with reverse traversal
set-matrix-zeroes
0
1
```\nclass Solution:\n def setZeroes(self, matrix: List[List[int]]) -> None:\n firstRowVal, R, C = 1, len(matrix), len(matrix[0])\n\n for i in range(R):\n for j in range(C):\n if matrix[i][j] == 0:\n matrix[0][j] = 0 # mark column\n if i !...
12
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. ...
🔥[Python3] O(1) with reverse traversal
set-matrix-zeroes
0
1
```\nclass Solution:\n def setZeroes(self, matrix: List[List[int]]) -> None:\n firstRowVal, R, C = 1, len(matrix), len(matrix[0])\n\n for i in range(R):\n for j in range(C):\n if matrix[i][j] == 0:\n matrix[0][j] = 0 # mark column\n if i !...
12
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. W...
Solution
set-matrix-zeroes
1
1
```C++ []\nclass Solution {\npublic:\n void setZeroes(vector<vector<int>>& mat) {\n int row=mat.size(),col=mat[0].size();\n int c=1;\n for(int i=0;i<row;i++){\n if(mat[i][0]==0)\n c=0;\n for(int j=1;j<col;j++){\n if(mat[i][j]==0){\n ...
4
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. ...
Solution
set-matrix-zeroes
1
1
```C++ []\nclass Solution {\npublic:\n void setZeroes(vector<vector<int>>& mat) {\n int row=mat.size(),col=mat[0].size();\n int c=1;\n for(int i=0;i<row;i++){\n if(mat[i][0]==0)\n c=0;\n for(int j=1;j<col;j++){\n if(mat[i][j]==0){\n ...
4
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. W...
Beats 99.93% users on Leetcode 🔥🔥
set-matrix-zeroes
0
1
# Intuition\nFind Address of All Zeroes and then all element respect the address becomes zero.\n\n# Approach\nstep 1 : First i create two lists:- row,col in which i stored the rows and colums where zero is present.\n\nstep 2 : go in evey row and change element to value zero\n\nstep 3 : go in evey coloumn and change ele...
1
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. ...
Beats 99.93% users on Leetcode 🔥🔥
set-matrix-zeroes
0
1
# Intuition\nFind Address of All Zeroes and then all element respect the address becomes zero.\n\n# Approach\nstep 1 : First i create two lists:- row,col in which i stored the rows and colums where zero is present.\n\nstep 2 : go in evey row and change element to value zero\n\nstep 3 : go in evey coloumn and change ele...
1
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. W...
Basic logic easy Code Python 3
set-matrix-zeroes
0
1
# Intuition\nIdentify which columns and rows that we need to make 0\n\n# Approach\nRows and Cols lists store the row no and col no which needs to be made 0\nUsing a for loop we check for the condition if the element is 0\nif it is we push its column and row number to the respective lists\n\n# Complexity\n- Time complex...
1
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. ...
Basic logic easy Code Python 3
set-matrix-zeroes
0
1
# Intuition\nIdentify which columns and rows that we need to make 0\n\n# Approach\nRows and Cols lists store the row no and col no which needs to be made 0\nUsing a for loop we check for the condition if the element is 0\nif it is we push its column and row number to the respective lists\n\n# Complexity\n- Time complex...
1
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. W...
Python thought process
set-matrix-zeroes
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nuse special char to notate zero and mark them as zero in 2nd pass\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(mn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n-...
1
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. ...
Python thought process
set-matrix-zeroes
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nuse special char to notate zero and mark them as zero in 2nd pass\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(mn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n-...
1
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. W...
Intuitive Python Solution - Constant Space | Beats 100%
set-matrix-zeroes
0
1
# Intuition\nThe first issue one runs into when solving this problem is figuring out how to not accidentally "override" the next row or column you wish to change to 0\'s. For example, If one were to look at the rows, and finds a 0, they could go through the row and change all the numbers to 0, but they might lose track...
1
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. ...
Intuitive Python Solution - Constant Space | Beats 100%
set-matrix-zeroes
0
1
# Intuition\nThe first issue one runs into when solving this problem is figuring out how to not accidentally "override" the next row or column you wish to change to 0\'s. For example, If one were to look at the rows, and finds a 0, they could go through the row and change all the numbers to 0, but they might lose track...
1
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. W...
🐍3 || FAST BEATS 96.44%🍔 || HALF MEMORY BEATS 56.23% 🧠
set-matrix-zeroes
0
1
# Explanation\nFirst, you want to find the coordinates where there are zeroes.\nThen, you want to change the rows and colums with the items at those coordinates\n\n# Code\n```\nclass Solution:\n def getChanges(self, mat):\n changePositions = []\n for i in range(len(mat)):\n for j in range(le...
2
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. ...
🐍3 || FAST BEATS 96.44%🍔 || HALF MEMORY BEATS 56.23% 🧠
set-matrix-zeroes
0
1
# Explanation\nFirst, you want to find the coordinates where there are zeroes.\nThen, you want to change the rows and colums with the items at those coordinates\n\n# Code\n```\nclass Solution:\n def getChanges(self, mat):\n changePositions = []\n for i in range(len(mat)):\n for j in range(le...
2
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. W...
Python easy solution with clear explanation with example
set-matrix-zeroes
0
1
# Intuition\nHere, our intuition is to make every row and column zero if there is a zero present in that row. To accomplish this, we need to follow these steps:\n\n# Approach\n\nStep 1: Append the indices to a list where there is a zero.\nFor example, given the list [[0,1,2,0],[3,4,5,2],[1,3,1,5]], the list of indices ...
4
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. ...
Python easy solution with clear explanation with example
set-matrix-zeroes
0
1
# Intuition\nHere, our intuition is to make every row and column zero if there is a zero present in that row. To accomplish this, we need to follow these steps:\n\n# Approach\n\nStep 1: Append the indices to a list where there is a zero.\nFor example, given the list [[0,1,2,0],[3,4,5,2],[1,3,1,5]], the list of indices ...
4
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. W...
Detailed solution in Python3/Go/TypeScript with O(mn) time and O(1) space complexity
set-matrix-zeroes
0
1
# Intuition\nWe can save space by simply marking the first row with zeroes at the columns for which we find zeroes on any row and doing the same for the first column.\n\nHowever, we will have a common cell at `[0][0]` that will then act as a marker for both, so we will let that act as an indicator for column and use an...
1
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. ...
Detailed solution in Python3/Go/TypeScript with O(mn) time and O(1) space complexity
set-matrix-zeroes
0
1
# Intuition\nWe can save space by simply marking the first row with zeroes at the columns for which we find zeroes on any row and doing the same for the first column.\n\nHowever, we will have a common cell at `[0][0]` that will then act as a marker for both, so we will let that act as an indicator for column and use an...
1
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). **Example 1:** **Input:** matrix = \[\[1,1,1\],\[1,0,1\],\[1,1,1\]\] **Output:** \[\[1,0,1\],\[0,0,0\],\[1,0,1\]\] **Example 2:** **In...
If any cell of the matrix has a zero we can record its row and column number using additional memory. But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. W...
Search a 2D Matrix | Binary Search | Telugu | Python | Amazon | Microsoft | Adobe | Paytm
search-a-2d-matrix
0
1
![Binary Search.png](https://assets.leetcode.com/users/images/3c1a047c-9071-4591-9a54-7f42b05ac653_1701104067.0182421.png)\n# Video Explanation : \n[https://youtu.be/5lgoHAKlBNo?si=p5wDI_E8Iw7o1305]()\n# Please Subscribe \uD83D\uDE0A\n\n\n\n# Complexity\n- Time complexity:$$O(log(n))$$\n<!-- Add your time complexity he...
1
You are given an `m x n` integer matrix `matrix` with the following two properties: * Each row is sorted in non-decreasing order. * The first integer of each row is greater than the last integer of the previous row. Given an integer `target`, return `true` _if_ `target` _is in_ `matrix` _or_ `false` _otherwise_. ...
null
Python3 Solution
search-a-2d-matrix
0
1
\n```\nclass Solution:\n def searchMatrix(self,matrix:List[List[int]],target:int)->bool:\n n=len(matrix)\n m=len(matrix[0])\n for i in range(n):\n for j in range(m):\n if matrix[i][j]==target:\n return True\n\n return False ...
1
You are given an `m x n` integer matrix `matrix` with the following two properties: * Each row is sorted in non-decreasing order. * The first integer of each row is greater than the last integer of the previous row. Given an integer `target`, return `true` _if_ `target` _is in_ `matrix` _or_ `false` _otherwise_. ...
null
BEATS 99.92%. PYTHON SIMPLE BINARY SEARCH
search-a-2d-matrix
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
You are given an `m x n` integer matrix `matrix` with the following two properties: * Each row is sorted in non-decreasing order. * The first integer of each row is greater than the last integer of the previous row. Given an integer `target`, return `true` _if_ `target` _is in_ `matrix` _or_ `false` _otherwise_. ...
null
Python3 Binary Search Solution
search-a-2d-matrix
0
1
# Intuition\nBinary Search\n\n# Approach\nBinary Search\n# Complexity\n- Time complexity:\nOlog(n*m)\n\n- Space complexity:\nO(1)\n# Code\n```\nclass Solution:\n def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:\n left = 0\n right = len(matrix)-1\n while left<=right:\n ...
1
You are given an `m x n` integer matrix `matrix` with the following two properties: * Each row is sorted in non-decreasing order. * The first integer of each row is greater than the last integer of the previous row. Given an integer `target`, return `true` _if_ `target` _is in_ `matrix` _or_ `false` _otherwise_. ...
null
✅ 100% Binary Search [VIDEO] - Simple Solution
search-a-2d-matrix
1
1
# Problem Understanding\nThe task is to find a target integer in a 2D matrix with the following properties: \n1. Each row is sorted in non-decreasing order.\n2. The first integer of each row is greater than the last integer of the previous row.\n\nThe challenge is to determine if the target integer exists within the ma...
98
You are given an `m x n` integer matrix `matrix` with the following two properties: * Each row is sorted in non-decreasing order. * The first integer of each row is greater than the last integer of the previous row. Given an integer `target`, return `true` _if_ `target` _is in_ `matrix` _or_ `false` _otherwise_. ...
null
One Liner Hack in 🐍 | 🔥Beats 86%🔥
search-a-2d-matrix
0
1
\n![image.png](https://assets.leetcode.com/users/images/79dfef21-fd38-4ecf-9b7b-97c976cc270a_1691391171.2960353.png)\n\n---\n# Complexity\n- Time complexity: $$O(m * n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(m * n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\...
1
You are given an `m x n` integer matrix `matrix` with the following two properties: * Each row is sorted in non-decreasing order. * The first integer of each row is greater than the last integer of the previous row. Given an integer `target`, return `true` _if_ `target` _is in_ `matrix` _or_ `false` _otherwise_. ...
null
Python | Easy Solution | Beats 90%
search-a-2d-matrix
0
1
# Intuition\nWe are applying Binary Search here.\n\n# Approach\nSame as binary search but in 2D.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nfrom typing import List\nclass Solution:\n def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:\n low=0\n ...
1
You are given an `m x n` integer matrix `matrix` with the following two properties: * Each row is sorted in non-decreasing order. * The first integer of each row is greater than the last integer of the previous row. Given an integer `target`, return `true` _if_ `target` _is in_ `matrix` _or_ `false` _otherwise_. ...
null
[Python3] ✅: Binary Search
search-a-2d-matrix
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\nThe problem statement basically gives the solution away. The problem statement made some very particular descriptions about the relationship [in terms of magnitude] between the first and last numbers of each row of the matrix. If you evaluate the impl...
1
You are given an `m x n` integer matrix `matrix` with the following two properties: * Each row is sorted in non-decreasing order. * The first integer of each row is greater than the last integer of the previous row. Given an integer `target`, return `true` _if_ `target` _is in_ `matrix` _or_ `false` _otherwise_. ...
null
Binary Search Logic Easy
search-a-2d-matrix
0
1
\n# Binary Search Approach---->O(LogN) \n```\nclass Solution:\n def searchMatrix(self, matrix: List[List[int]], target: int) -> \n row,col=len(matrix),len(matrix[0])\n left,right=0,row*col-1\n while left<=right:\n mid=(left+right)//2\n num=matrix[mid//col][mid%col]\n ...
39
You are given an `m x n` integer matrix `matrix` with the following two properties: * Each row is sorted in non-decreasing order. * The first integer of each row is greater than the last integer of the previous row. Given an integer `target`, return `true` _if_ `target` _is in_ `matrix` _or_ `false` _otherwise_. ...
null
Simple easy python solution using binary search!! beginner's friendly. 🙂🔥
search-a-2d-matrix
1
1
p# Code\n```\nclass Solution:\n def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:\n rows,columns=len(matrix),len(matrix[0])\n top,bottom=0,rows-1\n while top<=bottom:\n row=(top+bottom)//2\n if target<matrix[row][0]:\n bottom=row-1\n ...
4
You are given an `m x n` integer matrix `matrix` with the following two properties: * Each row is sorted in non-decreasing order. * The first integer of each row is greater than the last integer of the previous row. Given an integer `target`, return `true` _if_ `target` _is in_ `matrix` _or_ `false` _otherwise_. ...
null
Solution
sort-colors
1
1
```C++ []\nclass Solution {\npublic:\n void sortColors(vector<int>& nums) {\n int l = 0;\n int m = 0;\n int h = nums.size()-1;\n\n while(m<=h){\n if(nums[m]==0){\n swap(nums[l], nums[m]);\n l++;\n m++;\n }\n els...
465
Given an array `nums` with `n` objects colored red, white, or blue, sort them **[in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** so that objects of the same color are adjacent, with the colors in the order red, white, and blue. We will use the integers `0`, `1`, and `2` to represent the color red, white,...
A rather straight forward solution is a two-pass algorithm using counting sort. Iterate the array counting number of 0's, 1's, and 2's. Overwrite array with the total number of 0's, then 1's and followed by 2's.
0ms Solution Beats 100.00% C++
sort-colors
0
1
# Intuition\nThis is a similar problem to Move Zeroes to the end.\nWe just need to move the colors 1 and 2 at the end and 0 at the starting of the list\n\n# Approach\n1. Initialize two variables k1 and k2 as -1 and 0 respectively.\nHere, k1 is the left most index of 1 and k2 is the left most index of 2.\n2. Iterate thr...
2
Given an array `nums` with `n` objects colored red, white, or blue, sort them **[in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** so that objects of the same color are adjacent, with the colors in the order red, white, and blue. We will use the integers `0`, `1`, and `2` to represent the color red, white,...
A rather straight forward solution is a two-pass algorithm using counting sort. Iterate the array counting number of 0's, 1's, and 2's. Overwrite array with the total number of 0's, then 1's and followed by 2's.
Dutch Flag algorithm python
sort-colors
0
1
Simple implementation of Dutch flag algorithm in python\n\n# Complexity\n- Time complexity: O(n)\n\n# Code\n```\nclass Solution:\n def sortColors(self, arr: List[int]) -> None:\n n = len(arr)\n low = 0\n high = n - 1\n i = 0\n\n while i <= high:\n \n if arr[i]...
2
Given an array `nums` with `n` objects colored red, white, or blue, sort them **[in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** so that objects of the same color are adjacent, with the colors in the order red, white, and blue. We will use the integers `0`, `1`, and `2` to represent the color red, white,...
A rather straight forward solution is a two-pass algorithm using counting sort. Iterate the array counting number of 0's, 1's, and 2's. Overwrite array with the total number of 0's, then 1's and followed by 2's.
Beginner-friendly || Simple solution with HashMap+Prefix Sum in Python3 / TypeScript
sort-colors
0
1
# Intuition\nLet\'s briefly explain, what the problem is:\n- there a list of `nums`, it consists **only** with `0, 1`\'s and `2`\'s\n- our goal is to **sort** `nums` in **ascending order in-place**\n\nThe simplest approach we could use is to **apply built-in** sorting methods, but they\'re **not allowed**.\n\nSo, what ...
3
Given an array `nums` with `n` objects colored red, white, or blue, sort them **[in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** so that objects of the same color are adjacent, with the colors in the order red, white, and blue. We will use the integers `0`, `1`, and `2` to represent the color red, white,...
A rather straight forward solution is a two-pass algorithm using counting sort. Iterate the array counting number of 0's, 1's, and 2's. Overwrite array with the total number of 0's, then 1's and followed by 2's.
Solution
minimum-window-substring
1
1
```C++ []\nclass Solution {\npublic:\n string minWindow(string s, string t) {\n vector<int> map(128,0);\n for (char c : t) {\n map[c]++;\n }\n\n int counter = t.size(), begin = 0, end = 0, d = INT_MAX, head = 0;\n while (end < s.size()){\n if (map[s[end++]]-- ...
477
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Solution
minimum-window-substring
1
1
```C++ []\nclass Solution {\npublic:\n string minWindow(string s, string t) {\n vector<int> map(128,0);\n for (char c : t) {\n map[c]++;\n }\n\n int counter = t.size(), begin = 0, end = 0, d = INT_MAX, head = 0;\n while (end < s.size()){\n if (map[s[end++]]-- ...
477
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
[Python3] Sliding window O(N+M)
minimum-window-substring
0
1
\n# Approach\nThis problem follows the Sliding Window pattern and has a lot of similarities with [567 Permutation in a String](https://leetcode.com/problems/permutation-in-string/description/) with one difference. In this problem, we need to find a substring having all characters of the pattern which means that the req...
5
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
[Python3] Sliding window O(N+M)
minimum-window-substring
0
1
\n# Approach\nThis problem follows the Sliding Window pattern and has a lot of similarities with [567 Permutation in a String](https://leetcode.com/problems/permutation-in-string/description/) with one difference. In this problem, we need to find a substring having all characters of the pattern which means that the req...
5
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
✅ Explained - Simple and Clear Python3 Code✅
minimum-window-substring
0
1
\n# Approach\n\nThe solution utilizes a sliding window approach to find the minimum window substring in string s that includes all characters from string t.\n\nInitially, the algorithm initializes the necessary data structures to keep track of character counts. It iterates through s to identify the leftmost character i...
6
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
✅ Explained - Simple and Clear Python3 Code✅
minimum-window-substring
0
1
\n# Approach\n\nThe solution utilizes a sliding window approach to find the minimum window substring in string s that includes all characters from string t.\n\nInitially, the algorithm initializes the necessary data structures to keep track of character counts. It iterates through s to identify the leftmost character i...
6
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Min Window Substring, let me know if something is missing.
minimum-window-substring
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem seems to be about finding the minimum window in string `str1` that contains all characters of string `str2`. The idea is to use a sliding window approach, maintaining counts of characters in both strings and adjusting the wind...
0
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Min Window Substring, let me know if something is missing.
minimum-window-substring
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem seems to be about finding the minimum window in string `str1` that contains all characters of string `str2`. The idea is to use a sliding window approach, maintaining counts of characters in both strings and adjusting the wind...
0
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
O( n )✅ | Python (Step by step explanation)✅
minimum-window-substring
0
1
# Intuition\nThe problem requires finding the minimum window in string \'s\' that contains all characters from string \'t\'. We can use a sliding window approach to solve this problem efficiently.\n\n# Approach\n1. First, we handle the edge case: if \'t\' is an empty string, we return an empty string as the result.\n\n...
3
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
O( n )✅ | Python (Step by step explanation)✅
minimum-window-substring
0
1
# Intuition\nThe problem requires finding the minimum window in string \'s\' that contains all characters from string \'t\'. We can use a sliding window approach to solve this problem efficiently.\n\n# Approach\n1. First, we handle the edge case: if \'t\' is an empty string, we return an empty string as the result.\n\n...
3
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Step by step solution with O(N) time and O(1) space complexity
minimum-window-substring
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe most obvious way to do it is brute force with sliding window where we can check if all characters of t is present in a given window of string s where we slide the window on s , however we can optimize the solution using two hashmaps ...
2
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Step by step solution with O(N) time and O(1) space complexity
minimum-window-substring
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe most obvious way to do it is brute force with sliding window where we can check if all characters of t is present in a given window of string s where we slide the window on s , however we can optimize the solution using two hashmaps ...
2
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Simple Python sliding window solution with detailed explanation
minimum-window-substring
0
1
```\nfrom collections import Counter\n\nclass Solution:\n def minWindow(self, s: str, t: str) -> str:\n \'\'\'\n Keep t_counter of char counts in t\n \n We make a sliding window across s, tracking the char counts in s_counter\n We keep track of matches, the number of chars with mat...
111
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Simple Python sliding window solution with detailed explanation
minimum-window-substring
0
1
```\nfrom collections import Counter\n\nclass Solution:\n def minWindow(self, s: str, t: str) -> str:\n \'\'\'\n Keep t_counter of char counts in t\n \n We make a sliding window across s, tracking the char counts in s_counter\n We keep track of matches, the number of chars with mat...
111
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
✅🔥Sliding Window Approach with Explanation - C++/Java/Python
minimum-window-substring
1
1
\n# Intuition\nIn this problem, we need to find the minimum window substring of string `s` that contains all characters from string `t`. We can use a sliding window approach to find the minimum window substring efficiently.\n\n# Approach 01\n1. Create an unordered_map `mp` to store the count of characters in string `t`...
57
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
✅🔥Sliding Window Approach with Explanation - C++/Java/Python
minimum-window-substring
1
1
\n# Intuition\nIn this problem, we need to find the minimum window substring of string `s` that contains all characters from string `t`. We can use a sliding window approach to find the minimum window substring efficiently.\n\n# Approach 01\n1. Create an unordered_map `mp` to store the count of characters in string `t`...
57
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Simple python solution using two hash maps, beginner's friendly!! 💖🔥
minimum-window-substring
0
1
\n# Code\n```\nclass Solution:\n def minWindow(self, s: str, t: str) -> str:\n if t=="":\n return ""\n countT,hm={},{}\n for c in t:\n countT[c]=1+countT.get(c,0)\n \n have,need=0,len(countT)\n res=[-1,-1]\n reslen=float(\'infinity\')\n l=...
2
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Simple python solution using two hash maps, beginner's friendly!! 💖🔥
minimum-window-substring
0
1
\n# Code\n```\nclass Solution:\n def minWindow(self, s: str, t: str) -> str:\n if t=="":\n return ""\n countT,hm={},{}\n for c in t:\n countT[c]=1+countT.get(c,0)\n \n have,need=0,len(countT)\n res=[-1,-1]\n reslen=float(\'infinity\')\n l=...
2
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
C++ - Easiest Beginner Friendly Sol || Sliding window
minimum-window-substring
1
1
# Intuition of this Problem:\nReference - https://leetcode.com/problems/minimum-window-substring/solutions/26808/here-is-a-10-line-template-that-can-solve-most-substring-problems/?orderBy=hot\n<!-- Describe your first thoughts on how to solve this problem. -->\n**NOTE - PLEASE READ APPROACH FIRST THEN SEE THE CODE. YOU...
12
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
C++ - Easiest Beginner Friendly Sol || Sliding window
minimum-window-substring
1
1
# Intuition of this Problem:\nReference - https://leetcode.com/problems/minimum-window-substring/solutions/26808/here-is-a-10-line-template-that-can-solve-most-substring-problems/?orderBy=hot\n<!-- Describe your first thoughts on how to solve this problem. -->\n**NOTE - PLEASE READ APPROACH FIRST THEN SEE THE CODE. YOU...
12
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Python | My advice
minimum-window-substring
0
1
My advice for solving this problem is to:\n* Understand the intuition and what to do at a high level\n* Try to implement your own solution WITHOUT copying anyone elses\n* This is how you will learn\n* You will remember high level concepts, but never line for line code\n\nIntuition:\n* Two pointers, left and right\n* Bo...
62
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Python | My advice
minimum-window-substring
0
1
My advice for solving this problem is to:\n* Understand the intuition and what to do at a high level\n* Try to implement your own solution WITHOUT copying anyone elses\n* This is how you will learn\n* You will remember high level concepts, but never line for line code\n\nIntuition:\n* Two pointers, left and right\n* Bo...
62
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Python || Hashmap || Beats 98 % || Easy to understand
minimum-window-substring
0
1
# Approach\nFirst we will check the corner edge condititon. now create two hashmaps one that we need and one that we have and create one variable is to store window size and one list to store the window information. count the frequency of all the character that we need. we will also initalise a variable **have** as 0 a...
3
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Python || Hashmap || Beats 98 % || Easy to understand
minimum-window-substring
0
1
# Approach\nFirst we will check the corner edge condititon. now create two hashmaps one that we need and one that we have and create one variable is to store window size and one list to store the window information. count the frequency of all the character that we need. we will also initalise a variable **have** as 0 a...
3
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Python || Easy || Sliding Window || Dictionary
minimum-window-substring
0
1
```\nclass Solution:\n def minWindow(self, s: str, t: str) -> str:\n d=Counter(t)\n c=len(d)\n i=start=0\n n=len(s)\n ans=n+1\n for j in range(n):\n if s[j] in d:\n d[s[j]]-=1\n if d[s[j]]==0:\n c-=1 \n ...
4
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Python || Easy || Sliding Window || Dictionary
minimum-window-substring
0
1
```\nclass Solution:\n def minWindow(self, s: str, t: str) -> str:\n d=Counter(t)\n c=len(d)\n i=start=0\n n=len(s)\n ans=n+1\n for j in range(n):\n if s[j] in d:\n d[s[j]]-=1\n if d[s[j]]==0:\n c-=1 \n ...
4
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Python | Sliding Window | With Explanation | Easy to Understand
minimum-window-substring
0
1
```\nclass Solution:\n def minWindow(self, s: str, t: str) -> str:\n target_counter = Counter(t)\n left = 0\n min_length = float(\'inf\')\n substring = ""\n target_length = len(t)\n \n for right in range(len(s)):\n # if we find a letter in t\n if...
2
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Python | Sliding Window | With Explanation | Easy to Understand
minimum-window-substring
0
1
```\nclass Solution:\n def minWindow(self, s: str, t: str) -> str:\n target_counter = Counter(t)\n left = 0\n min_length = float(\'inf\')\n substring = ""\n target_length = len(t)\n \n for right in range(len(s)):\n # if we find a letter in t\n if...
2
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Easy Approach with Explanation: 97.81% faster than others
minimum-window-substring
0
1
Step 1: Create a Counter Dictionary{name: countT} for Substring t\nStep 2: Define have = 0, need=len(t)\nStep 3: Create a Hash Map(Dictionary){name: window} and Traverse String s and a left ptr l = 0.\n ->when countT[char] == window[char]: have += 1\n \n ->when have == need: {we got a subtring in a string}\n ...
3
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Easy Approach with Explanation: 97.81% faster than others
minimum-window-substring
0
1
Step 1: Create a Counter Dictionary{name: countT} for Substring t\nStep 2: Define have = 0, need=len(t)\nStep 3: Create a Hash Map(Dictionary){name: window} and Traverse String s and a left ptr l = 0.\n ->when countT[char] == window[char]: have += 1\n \n ->when have == need: {we got a subtring in a string}\n ...
3
Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window**_ **_substring_** _of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window_. If there is no such substring, return _the empty string_ `" "`. The testcases will be generated such tha...
Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is als...
Python easy solutions || time & memory efficient
combinations
0
1
# Intuition\nThe problem requires generating all combinations of length k from the integers from 1 to n. To achieve this, we can use the itertools.combinations function, which will efficiently generate all possible combinations for us.\n\n# Approach\nWe will use the itertools.combinations function from the Python stand...
1
Given two integers `n` and `k`, return _all possible combinations of_ `k` _numbers chosen from the range_ `[1, n]`. You may return the answer in **any order**. **Example 1:** **Input:** n = 4, k = 2 **Output:** \[\[1,2\],\[1,3\],\[1,4\],\[2,3\],\[2,4\],\[3,4\]\] **Explanation:** There are 4 choose 2 = 6 total combin...
null
Python 1 line, use the tool you have
combinations
0
1
# Code\n```\nclass Solution:\n def combine(self, n: int, k: int) -> List[List[int]]:\n return list(combinations(range(1, n+1), k))\n```\n\n# Code (For interview)\n```\nclass Solution:\n def combine(self, n: int, k: int) -> List[List[int]]:\n pool = tuple(range(1, n+1))\n n = len(pool)\n ...
1
Given two integers `n` and `k`, return _all possible combinations of_ `k` _numbers chosen from the range_ `[1, n]`. You may return the answer in **any order**. **Example 1:** **Input:** n = 4, k = 2 **Output:** \[\[1,2\],\[1,3\],\[1,4\],\[2,3\],\[2,4\],\[3,4\]\] **Explanation:** There are 4 choose 2 = 6 total combin...
null
✔️Python||C||C++🔥99%✅Backtrack with graph explained || Beginner-friendly ^_^
combinations
0
1
# Intuition\nThis is a backtracking problem.\nWe made a recursion function, and get all combinations by take or not take specific number.\n\n# Approach\n## variables\n`nums` is the current combination.\n`pos` is the current position in `nums`\n`cur` is the current number\n\nIn the function `backtrack`:\nIf `pos == k`, ...
16
Given two integers `n` and `k`, return _all possible combinations of_ `k` _numbers chosen from the range_ `[1, n]`. You may return the answer in **any order**. **Example 1:** **Input:** n = 4, k = 2 **Output:** \[\[1,2\],\[1,3\],\[1,4\],\[2,3\],\[2,4\],\[3,4\]\] **Explanation:** There are 4 choose 2 = 6 total combin...
null
🧩 Iterative & Backtracking [VIDEO] 100% Efficient Combinatorial Generation
combinations
1
1
# Intuition\nGiven two integers `n` and `k`, the task is to generate all possible combinations of `k` numbers from the range `[1, n]`. The initial intuition to solve this problem is to leverage the concept of combination generation, where we iteratively choose `k` numbers from `n` numbers without any repetition. Howeve...
83
Given two integers `n` and `k`, return _all possible combinations of_ `k` _numbers chosen from the range_ `[1, n]`. You may return the answer in **any order**. **Example 1:** **Input:** n = 4, k = 2 **Output:** \[\[1,2\],\[1,3\],\[1,4\],\[2,3\],\[2,4\],\[3,4\]\] **Explanation:** There are 4 choose 2 = 6 total combin...
null
Three Optimal Solutions (Python)
combinations
0
1
Each of these solutions defines `solution.combine` as a generator instead of having it return a list. This approach is accepted, it\'s a bit cleaner, and it\'s probably a little bit faster.\n##### Recursive:\n```\nclass Solution:\n def combine(self, n: int, k: int) -> List[List[int]]:\n if k == 0:\n ...
1
Given two integers `n` and `k`, return _all possible combinations of_ `k` _numbers chosen from the range_ `[1, n]`. You may return the answer in **any order**. **Example 1:** **Input:** n = 4, k = 2 **Output:** \[\[1,2\],\[1,3\],\[1,4\],\[2,3\],\[2,4\],\[3,4\]\] **Explanation:** There are 4 choose 2 = 6 total combin...
null
Python Code 🐍(Only 1 Line😮😮😮) Beats 99.89%🤩
combinations
0
1
\n\n# Code\n```\nfrom itertools import combinations\nclass Solution:\n def combine(self, n: int, k: int) -> List[List[int]]:\n return list(combinations(range(1, n+1), k))\n\n```\n# ***Upvote my Solution if u learned something new today***
1
Given two integers `n` and `k`, return _all possible combinations of_ `k` _numbers chosen from the range_ `[1, n]`. You may return the answer in **any order**. **Example 1:** **Input:** n = 4, k = 2 **Output:** \[\[1,2\],\[1,3\],\[1,4\],\[2,3\],\[2,4\],\[3,4\]\] **Explanation:** There are 4 choose 2 = 6 total combin...
null
Python Easy Solution || 100% ||
combinations
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
Given two integers `n` and `k`, return _all possible combinations of_ `k` _numbers chosen from the range_ `[1, n]`. You may return the answer in **any order**. **Example 1:** **Input:** n = 4, k = 2 **Output:** \[\[1,2\],\[1,3\],\[1,4\],\[2,3\],\[2,4\],\[3,4\]\] **Explanation:** There are 4 choose 2 = 6 total combin...
null
Ex-Amazon explains a solution with a video, Python, JavaScript, Java and C++
combinations
1
1
# Intuition\nUsing backtracking to create all possible combinations.\n\n---\n\n# Solution Video\n## *** Please upvote for this article. *** \n\nhttps://youtu.be/oDn_6n0ahkM\n\n# Subscribe to my channel from here. I have 237 videos as of August 1st\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmatio...
15
Given two integers `n` and `k`, return _all possible combinations of_ `k` _numbers chosen from the range_ `[1, n]`. You may return the answer in **any order**. **Example 1:** **Input:** n = 4, k = 2 **Output:** \[\[1,2\],\[1,3\],\[1,4\],\[2,3\],\[2,4\],\[3,4\]\] **Explanation:** There are 4 choose 2 = 6 total combin...
null