question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
set-matrix-zeroes | Two Different Approaches | two-different-approaches-by-ganjinaveen-08yq | 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 | GANJINAVEEN | NORMAL | 2023-08-22T06:31:17.521938+00:00 | 2023-08-22T06:33:24.988887+00:00 | 3,013 | false | # 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... | 14 | 0 | ['Python', 'Python3'] | 5 |
set-matrix-zeroes | ✅ EASY C++ O(1) FAANG😱 OPTIMAL APPROACH💥 | easy-c-o1-faang-optimal-approach-by-adit-snr9 | Solution 1:\n \n# Approach: Using brute force\n\nAssuming all the elements in the matrix are non-negative. Traverse through the matrix and if you find an elemen | AdityaBhate | NORMAL | 2022-08-02T04:34:12.151788+00:00 | 2022-08-02T04:35:16.674627+00:00 | 3,256 | false | # **Solution 1:**\n \n# **Approach: Using brute force**\n\nAssuming all the elements in the matrix are non-negative. Traverse through the matrix and if you find an element with value 0, then change all the elements in its row and column to -1, except when an element is 0. The reason for not changing other elements to 0... | 14 | 1 | ['C', 'Matrix', 'C++'] | 1 |
set-matrix-zeroes | ✔️ 100% Fastest Swift Solution | 100-fastest-swift-solution-by-sergeylesc-bqut | \nclass Solution {\n func setZeroes(_ matrix: inout [[Int]]) {\n let row = matrix.count\n let col = matrix[0].count\n \n for i in | sergeyleschev | NORMAL | 2022-04-06T05:39:20.099467+00:00 | 2022-04-06T05:39:20.099517+00:00 | 902 | false | ```\nclass Solution {\n func setZeroes(_ matrix: inout [[Int]]) {\n let row = matrix.count\n let col = matrix[0].count\n \n for i in 0..<row {\n for j in 0..<col {\n if matrix[i][j] == 0 {\n setMax(&matrix, i, j)\n }\n ... | 14 | 0 | ['Swift'] | 2 |
set-matrix-zeroes | Java solution for beginners. | java-solution-for-beginners-by-kunal3322-p148 | *** Please upvote if helpful!**\n\npublic void setZeroes(int[][] matrix) {\n int[] row = new int[matrix.length];\n int[] col = new int[matrix[0].l | kunal3322 | NORMAL | 2021-07-04T22:21:24.493350+00:00 | 2021-07-04T22:21:24.493387+00:00 | 624 | false | *** Please upvote if helpful!**\n```\npublic void setZeroes(int[][] matrix) {\n int[] row = new int[matrix.length];\n int[] col = new int[matrix[0].length];\n\n for (int i = 0; i < matrix.length; i++) {\n for (int j = 0; j < matrix[0].length; j++) {\n if (matrix[i][j] == 0... | 14 | 0 | ['Java'] | 1 |
set-matrix-zeroes | JAVA ✔✔|| Striver 🔥🔥|| Easy-understanding | java-striver-easy-understanding-by-ydvaa-leg6 | \n# 1. Brute force Approach\nComplexity\nTime complexity:O((NM)(N + M)) + O(N*M), where N = no. of rows in the matrix and M = no. of columns in the matrix.\nSpa | ydvaaman | NORMAL | 2023-06-27T13:37:52.598875+00:00 | 2023-07-01T01:42:52.896792+00:00 | 4,366 | false | ```\n# 1. Brute force Approach\nComplexity\nTime complexity:O((NM)(N + M)) + O(N*M), where N = no. of rows in the matrix and M = no. of columns in the matrix.\nSpace complexity:O(1)\n\n# Code\n\nclass Solution {\n \n // making rows negative excluding 0\'s\n public static void makeRow(int matrix[][],int n,int m... | 12 | 0 | ['Java'] | 6 |
set-matrix-zeroes | With Explanation: 0(1) space && 0(m+n) space | with-explanation-01-space-0mn-space-by-1-jlfs | \n\n//O(m+n) space, O(m+n) time\n//keep two sets, one for row indexes than should be marked 0, and one for col indexes that should be marked 0\n//loop through m | 11grossmane | NORMAL | 2021-01-09T19:07:28.904762+00:00 | 2021-01-09T19:10:18.931301+00:00 | 1,526 | false | ```\n\n//O(m+n) space, O(m+n) time\n//keep two sets, one for row indexes than should be marked 0, and one for col indexes that should be marked 0\n//loop through matrix once, populating the sets\n//loop through a second time, setting items to 0, if their indexes are in either set\n\nconst setZeroes = (matrix) => {\n ... | 12 | 0 | ['JavaScript'] | 3 |
set-matrix-zeroes | Java Best Explained O(1) space complexity | java-best-explained-o1-space-complexity-aaoi8 | \nclass Solution {\n public void setZeroes(int[][] matrix) {\n //if we do normal approach whole matrix becomes 0\n // Basic explanation:\n | user1233l | NORMAL | 2020-07-01T20:37:45.818095+00:00 | 2020-07-01T20:37:45.818148+00:00 | 1,146 | false | ```\nclass Solution {\n public void setZeroes(int[][] matrix) {\n //if we do normal approach whole matrix becomes 0\n // Basic explanation:\n // we are using the first row and column as a memory to keep track of all the 0\'s in the entire matrix.\n \n if(matrix==null || matrix.leng... | 12 | 0 | ['Java'] | 2 |
set-matrix-zeroes | 3 approaches from brute to optimal | 3-approaches-from-brute-to-optimal-by-sh-v36i | Intuition\n Describe your first thoughts on how to solve this problem. \nTo traverse through thw whole matrix and if a zero is found then set whole row and colu | shreyajain1808 | NORMAL | 2023-06-04T14:03:04.263029+00:00 | 2023-06-04T14:03:04.263071+00:00 | 2,170 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo traverse through thw whole matrix and if a zero is found then set whole row and column to 0\n# Approach 1:\n<!-- Describe your approach to solving the problem. -->\nTraversing the matrix and if 0 is found then setting it to a negative ... | 11 | 0 | ['C++'] | 3 |
set-matrix-zeroes | Python [Easy] Solution | python-easy-solution-by-gopibhoyar3-07js | I have initialize the coordinate list for row and col. If we found zero we append its row and col coordinates to respective list....Easy??\n\nFor the next steps | gopibhoyar3 | NORMAL | 2022-07-05T20:21:59.928929+00:00 | 2022-07-05T20:21:59.928968+00:00 | 907 | false | I have initialize the coordinate list for row and col. If we found zero we append its row and col coordinates to respective list....Easy??\n\nFor the next steps I\'m simply adding zero to (0-R, 1-R, 2-R, ...upto no. of rows) indexes. Similarly for the column I\'m adding zero to (C-0, C-1, C-2, ...upto no of columns) in... | 11 | 0 | ['Array', 'Matrix', 'Python'] | 1 |
set-matrix-zeroes | C++ || 3 approaches || Easy-to-understand | c-3-approaches-easy-to-understand-by-jun-26aw | It is a frequently asked problem in interviews and a really good example of how to reduce time and space complexity for matrices using certain observations.\n\n | jungfreud | NORMAL | 2022-04-18T18:55:18.369784+00:00 | 2022-06-07T05:48:08.152086+00:00 | 866 | false | It is a frequently asked problem in interviews and a really good example of how to reduce time and space complexity for matrices using certain observations.\n\n**1st approach: Brute Force**\n\nSo, this approach would cross everyone\'s mind and goes as following-\nFirst we\'ll check for cells with zero and then create a... | 11 | 0 | ['C'] | 8 |
set-matrix-zeroes | Keep map[row]=[col0, col1] which has ele=0 in it, Runtime- 0 ms Beats 100.00%, Memory- Beats 29.25% | keep-maprowcol0-col1-which-has-ele0-in-i-l3y9 | ApproachFirst iterate through matrix and generate map[row][]cols, which will keep row as key and all columns which has 0 in it as values, then iterate through t | sushilmaxbhile | NORMAL | 2025-03-28T07:14:30.680637+00:00 | 2025-03-28T07:14:30.680637+00:00 | 423 | false | # Approach
First iterate through matrix and generate map[row][]cols, which will keep row as key and all columns which has 0 in it as values, then iterate through this map and replace rows and colums values to 0
# Complexity
- Time complexity:
0 ms Beats 100.00%
- Space complexity:
7.91 MB Beats 29.25%
# Code
```gola... | 10 | 1 | ['Go'] | 0 |
set-matrix-zeroes | Super Easy Approach | super-easy-approach-by-shubhamykp2003-z79l | \n# Approach\n Describe your approach to solving the problem. \nMark all the rows and columns in which the zero is present in matrix as all the elements would b | shubhamykp2003 | NORMAL | 2024-06-01T16:31:34.541065+00:00 | 2024-06-01T16:31:34.541086+00:00 | 1,077 | false | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nMark all the rows and columns in which the zero is present in matrix as all the elements would be zero int that particular column and row.\n\n# Complexity\n- Time complexity: O(2*n*m)=O(n*m)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n... | 10 | 0 | ['Java'] | 3 |
set-matrix-zeroes | [C++] Easy, Clean Solution O(1) Implementation | c-easy-clean-solution-o1-implementation-xi8a2 | Solution:\n\nApproach\n1. First check if there is any 0 in the first row or column and set the respective flag(row, col) to 1.\n2. Now, iterate from row 2 and c | rsgt24 | NORMAL | 2021-08-13T07:41:27.800393+00:00 | 2021-08-13T14:35:29.459403+00:00 | 1,075 | false | **Solution:**\n\nApproach\n1. First check if there is any `0` in the first row or column and set the respective flag(`row, col`) to `1`.\n2. Now, iterate from `row 2` and `column 2` and check if there is any zero present; fill the first row index `ar[0][j] = 0` and first column index `ar[i][0] = 0`.\n3. Our first row a... | 10 | 3 | ['C', 'Matrix'] | 0 |
set-matrix-zeroes | Python O(1) aux space sol by bit masking. 72%+ [ With explanation ] | python-o1-aux-space-sol-by-bit-masking-7-7ykv | Python O(1) aux space sol by bit masking\n\nLet m, n denote as the dimension of matrix height and width.\n\n---\n\nHint:\nWhat we need to know is index of row a | brianchiang_tw | NORMAL | 2020-02-18T13:51:19.182766+00:00 | 2020-02-21T09:11:11.856976+00:00 | 1,110 | false | Python O(1) aux space sol by bit masking\n\nLet *m*, *n* denote as the dimension of matrix height and width.\n\n---\n\n**Hint**:\nWhat we need to know is **index of row** and **index column** for **zero element**.\n\nUsually, first idea pop into our head is to to store those indices in a set, which is up to O( m + n )\... | 10 | 0 | ['Bitmask', 'Python'] | 3 |
set-matrix-zeroes | JavaScript - O(n * m) time, O(1) space, first row/col flags | javascript-on-m-time-o1-space-first-rowc-1fcj | javascript\n/**\n * Time: O(n * m)\n * Space: O(1)\n * n - number of rows in matrix\n * m - number of cols in matrix\n */\n\n/**\n * @param {number[][]} matrix\ | jwu1100 | NORMAL | 2018-12-24T10:11:42.810983+00:00 | 2018-12-24T10:11:42.811026+00:00 | 798 | false | ```javascript\n/**\n * Time: O(n * m)\n * Space: O(1)\n * n - number of rows in matrix\n * m - number of cols in matrix\n */\n\n/**\n * @param {number[][]} matrix\n * @return {void} Do not return anything, modify matrix in-place instead.\n */\nfunction setZeroes(matrix) {\n let firstColHasZero = false;\n let firstRow... | 10 | 2 | [] | 2 |
set-matrix-zeroes | Efficient Solution to Set Zeroes in a Matrix | efficient-solution-to-set-zeroes-in-a-ma-pyhq | Intuition\n Describe your first thoughts on how to solve this problem. \nTo solve this problem, we can iterate through the given matrix and keep track of the ro | pro_donkey | NORMAL | 2024-03-19T16:57:20.141679+00:00 | 2024-03-19T16:57:20.141712+00:00 | 2,030 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo solve this problem, we can iterate through the given matrix and keep track of the rows and columns that contain zeros. Once we have identified the rows and columns to be zeroed out, we can update the corresponding elements in the matri... | 9 | 0 | ['Java', 'Python3'] | 1 |
set-matrix-zeroes | Shortest solution using HashSet | O(m + n) space | shortest-solution-using-hashset-om-n-spa-lk3w | Approach\n Describe your approach to solving the problem. \n- Here whenever we find the 0 element in the matrix we add its row and column to the relative set.\ | Aniket_1104 | NORMAL | 2023-06-04T07:32:53.400415+00:00 | 2023-06-29T19:24:41.221182+00:00 | 4,493 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\n- Here whenever we find the 0 element in the matrix we add its row and column to the relative set.\n- Since its a set data structure , no two same row or column will be added.\n- Then we iterate through each row and column in the set and fill the mat... | 9 | 1 | ['Hash Table', 'Java'] | 2 |
set-matrix-zeroes | ✅STRIVER'S SOLUTION ||Upvote if you like ⬆️⬆️ | strivers-solution-upvote-if-you-like-by-frtk8 | Intuition\nusing striver approach\n\n# Approach\nstore states of each row in the first of that row, and store states of each column in the first of that column. | ratnesh_maurya | NORMAL | 2022-11-11T06:51:23.370725+00:00 | 2022-12-08T19:05:48.331706+00:00 | 1,216 | false | # Intuition\nusing striver approach\n\n# Approach\nstore states of each row in the first of that row, and store states of each column in the first of that column. Because the state of row0 and the state of column0 would occupy the same cell, I let it be the state of row0, and use another variable "colm" for column0. In... | 9 | 0 | ['C++'] | 1 |
set-matrix-zeroes | [VIDEO] Visualization of O(1) Space Solution | video-visualization-of-o1-space-solution-9ghx | https://www.youtube.com/watch?v=UdKuMlVkk5A\n\nWe can\'t set the row and column zeroes as we\'re traversing the matrix because then we would confuse the origina | AlgoEngine | NORMAL | 2024-08-30T02:56:35.707486+00:00 | 2024-08-30T02:56:35.707504+00:00 | 1,312 | false | https://www.youtube.com/watch?v=UdKuMlVkk5A\n\nWe can\'t set the row and column zeroes as we\'re traversing the matrix because then we would confuse the <i>original</i> zeroes with the ones that we set later on. One workaround is to create a copy of the matrix. Then we could traverse the copy, and every time we hit a... | 8 | 0 | ['Array', 'Matrix', 'Python', 'Python3'] | 1 |
set-matrix-zeroes | ✅✅FULL DETAILED EXPLANATION ||4 steps✅✅ | full-detailed-explanation-4-steps-by-abh-znou | Intuition\n Describe your first thoughts on how to solve this problem. \n# guys upvote , i write so much in depth putting so much time and you guys dont even up | Abhishekkant135 | NORMAL | 2024-04-10T19:28:27.925194+00:00 | 2024-04-10T19:28:27.925232+00:00 | 600 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n# guys upvote , i write so much in depth putting so much time and you guys dont even upvote . Shame on you ppl. You will be punished . IMMORAL, IRRELIGIOUS.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n\n**Steps... | 8 | 0 | ['Matrix', 'Java'] | 1 |
set-matrix-zeroes | Simplest C++ solution using set and for loop | simplest-c-solution-using-set-and-for-lo-nfjp | 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 | vishu_0123 | NORMAL | 2023-02-07T13:45:44.040436+00:00 | 2023-02-07T13:45:44.040462+00:00 | 1,087 | false | # 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)$$ --... | 8 | 0 | ['C++'] | 2 |
set-matrix-zeroes | ✅Set Matrix Zeroes || C++ easy and clear cut explanation. | set-matrix-zeroes-c-easy-and-clear-cut-e-6win | \nclass Solution {\npublic:\n //Just keep it very simple \n //if any row or col containing zeros that means we have to set that row or col to zero.\n v | aman_2_0_2_3 | NORMAL | 2022-09-26T19:54:43.684633+00:00 | 2022-09-26T19:54:43.684670+00:00 | 2,149 | false | ```\nclass Solution {\npublic:\n //Just keep it very simple \n //if any row or col containing zeros that means we have to set that row or col to zero.\n void setZeroes(vector<vector<int>>& matrix){\n bool isrow = false, iscol = false;\n for(int i=0;i<matrix.size();i++){\n for(int j=0;j... | 8 | 0 | ['C', 'C++'] | 0 |
set-matrix-zeroes | O(1) Space || Easily explained || Faster than 100% | o1-space-easily-explained-faster-than-10-8nj7 | Code :\n\nvoid setZeroes(vector<vector<int>>& matrix) \n{\n int m = matrix.size(), n = matrix[0].size();\n bool isRowZero = false, isColZero = fal | bhagirath7733 | NORMAL | 2022-04-21T05:21:21.029692+00:00 | 2022-05-02T03:49:54.185885+00:00 | 816 | false | **Code :**\n```\nvoid setZeroes(vector<vector<int>>& matrix) \n{\n int m = matrix.size(), n = matrix[0].size();\n bool isRowZero = false, isColZero = false;\n \n\t\t// Check the first Column, zero is present or not...\n for(int i = 0; i < m; i++)\n {\n if(matrix[i][0] == 0)... | 8 | 0 | ['C', 'Matrix'] | 2 |
set-matrix-zeroes | 𝗘𝗔𝗦𝗬 𝗨𝗡𝗗𝗘𝗥𝗦𝗧𝗔𝗡𝗗𝗜𝗡𝗚 || 𝗖++|| 𝟬(m+n)|| | easy-understanding-c-0mn-by-prabhatpradh-1v1l | ```\nclass Solution {\npublic:\n void setZeroes(vector>& A) {\n const int m = A.size(), n = A[0].size();\n vector row(m, 1), col(n,1);\n | prabhatpradhan097 | NORMAL | 2021-09-04T13:23:50.558808+00:00 | 2021-09-04T14:29:18.785739+00:00 | 884 | false | ```\nclass Solution {\npublic:\n void setZeroes(vector<vector<int>>& A) {\n const int m = A.size(), n = A[0].size();\n vector<int> row(m, 1), col(n,1);\n for(int i =0; i < m; i++)\n for(int j = 0; j < n; j++)\n if(A[i][j] == 0){\n col[j] = 0;\n ... | 8 | 0 | ['C'] | 1 |
set-matrix-zeroes | ✅2 Approach w/ Explanation || O(M+N) | O(1) || C++ | Python | Java | 2-approach-w-explanation-omn-o1-c-python-jly5 | OBSERVATION:\nThe time complexity of this problem remains O(M*N), the only improvement we can do is of the space complexity. So we will have 2 approaches here\n | Maango16 | NORMAL | 2021-08-13T07:39:44.558674+00:00 | 2021-08-13T07:39:44.558717+00:00 | 472 | false | **OBSERVATION:**\nThe time complexity of this problem remains `O(M*N)`, the only improvement we can do is of the space complexity. So we will have 2 approaches here\n\n# **APPROACH I:**\n*Additional Memory Approach-*\nIf any cell of the matrix has a zero we can record its row and column number. All the cells of this re... | 8 | 0 | [] | 2 |
set-matrix-zeroes | [C++] Clean O(1) space sol. [Detailed Explanation] | c-clean-o1-space-sol-detailed-explanatio-3z3x | \n/*\n \n https://leetcode.com/problems/set-matrix-zeroes/solution/\n \n Idea is to use 1st row and 1st col to save the 0 reset status\n for the | cryptx_ | NORMAL | 2021-03-24T05:51:36.082771+00:00 | 2021-03-24T05:51:36.082803+00:00 | 1,071 | false | ```\n/*\n \n https://leetcode.com/problems/set-matrix-zeroes/solution/\n \n Idea is to use 1st row and 1st col to save the 0 reset status\n for the rows and cols. Now since position [0][0] will overlap\n between the row and col status vector, we use 2 separate variables \n just to save whether 1st ... | 8 | 1 | ['C', 'C++'] | 2 |
set-matrix-zeroes | JavaScript solution memory usage beats 90 % | javascript-solution-memory-usage-beats-9-szt8 | \nvar setZeroes = function(matrix) {\n let xs = new Set();\n let ys = new Set();\n for (let i = 0; i < matrix.length; i++) {\n for (let j = 0; j < matrix[ | tiokang | NORMAL | 2019-09-26T07:10:58.635595+00:00 | 2019-09-26T07:10:58.635640+00:00 | 1,329 | false | ```\nvar setZeroes = function(matrix) {\n let xs = new Set();\n let ys = new Set();\n for (let i = 0; i < matrix.length; i++) {\n for (let j = 0; j < matrix[i].length; j++) {\n if (matrix[i][j]) {\n continue;\n } else {\n xs.add(i);\n ys.add(j);\n }\n }\n }\n for (let i = ... | 8 | 2 | ['JavaScript'] | 3 |
set-matrix-zeroes | Three Solutions in Python 3 ( O(1), O(M+N), and O(MN) space ) | three-solutions-in-python-3-o1-omn-and-o-mcm5 | O(1) space:\n\nclass Solution:\n def setZeroes(self, m: List[List[int]]) -> None:\n \tM, N = len(m), len(m[0])\n \tfor i,j in itertools.product(range(M | junaidmansuri | NORMAL | 2019-09-15T22:59:19.671186+00:00 | 2019-09-16T01:39:24.848869+00:00 | 2,187 | false | _O(1) space:_\n```\nclass Solution:\n def setZeroes(self, m: List[List[int]]) -> None:\n \tM, N = len(m), len(m[0])\n \tfor i,j in itertools.product(range(M),range(N)):\n \t\tif m[i][j]: continue\n \t\tfor k in range(N):\n \t\t\tif m[i][k] != 0: m[i][k] = \' \'\n \t\tfor k in range(M):\n \t\t\ti... | 8 | 0 | ['Python', 'Python3'] | 3 |
set-matrix-zeroes | JAVA constant space solution (Hint: use space inside the matrix) | java-constant-space-solution-hint-use-sp-ayz9 | An easy way to solve this problem is to use extra O(m + n) space, storing the zero row and column indices. \n\nWe can improve it by not using the extra O(m + n) | zkfairytale | NORMAL | 2014-11-20T21:08:36+00:00 | 2014-11-20T21:08:36+00:00 | 1,989 | false | An easy way to solve this problem is to use extra O(m + n) space, storing the zero row and column indices. \n\nWe can improve it by not using the extra O(m + n) space, instead, we can use the space inside that input matrix (inspired by **Shangrila**'s solution, which use the first row and column for storage).\n\nIn thi... | 8 | 1 | ['Java'] | 0 |
set-matrix-zeroes | Python solution with detailed explanation | python-solution-with-detailed-explanatio-aoea | Solution\n\nSet Matrix Zeroes https://leetcode.com/problems/set-matrix-zeroes/\n\nAlgorithm\n1. Encode the state of each row in matrix[i,0].\n2. Encode the stat | gabbu | NORMAL | 2017-02-14T21:56:51.960000+00:00 | 2017-02-14T21:56:51.960000+00:00 | 891 | false | **Solution**\n\n**Set Matrix Zeroes** https://leetcode.com/problems/set-matrix-zeroes/\n\n**Algorithm**\n1. Encode the state of each row in matrix[i,0].\n2. Encode the state of each col in matrix[0,j].\n3. During steps 1 and 2, maintain two boolean variables to keep status of row 0 and column 0. Why do we need these va... | 8 | 0 | [] | 0 |
set-matrix-zeroes | ✅ 100% Runtime| 95.93% Memory| 3 solutions : Brute force ->Better ->Optimal ✅O(n*m) ->O(n+m)-> O(1) | 100-runtime-9593-memory-3-solutions-brut-b5rk | Here are three solutions, arranged from brute force to the optimal approach, based on their time and space complexity. 🚀🔢Solution 1: Using Extra Matrix (Brute F | himanshukansal101 | NORMAL | 2025-02-27T12:13:23.160037+00:00 | 2025-02-27T12:13:23.160037+00:00 | 1,011 | false | ## *`Here are three solutions, arranged from brute force to the optimal approach, based on their time and space complexity. 🚀🔢`*
---
# Solution 1: Using Extra Matrix (Brute Force) 🏗️💾
# 🧠 Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We create a duplicate matrix 🏭 of the same siz... | 7 | 1 | ['Array', 'Matrix', 'C++'] | 1 |
set-matrix-zeroes | Simple Solution with Diagrams in Video - JavaScript, C++, Java, Python | simple-solution-with-diagrams-in-video-j-pzh0 | Video\nPlease upvote here so others save time too!\n\nLike the video on YouTube if you found it useful\n\nClick here to subscribe on YouTube:\nhttps://www.youtu | danieloi | NORMAL | 2024-11-25T12:29:25.691128+00:00 | 2024-11-25T12:29:25.691167+00:00 | 1,549 | false | # Video\nPlease upvote here so others save time too!\n\nLike the video on YouTube if you found it useful\n\nClick here to subscribe on YouTube:\nhttps://www.youtube.com/@mayowadan?sub_confirmation=1\n\nThanks!\n\nhttps://youtu.be/5TfyWZc_y-E\n\n```javascript []\n/**\n * @param {number[][]} mat\n * @return {void} Do not... | 7 | 0 | ['Python', 'C++', 'Java', 'Python3', 'JavaScript'] | 0 |
set-matrix-zeroes | Beats 100% || Super elaborative and helpful solution||JAVA | beats-100-super-elaborative-and-helpful-luzca | Intuition\nTo solve the problem of setting entire rows and columns to zeroes where any element is zero, the idea is to first identify which rows and columns nee | theDummy | NORMAL | 2024-09-05T16:17:57.564556+00:00 | 2024-09-05T16:17:57.564589+00:00 | 1,416 | false | # Intuition\nTo solve the problem of setting entire rows and columns to zeroes where any element is zero, the idea is to first identify which rows and columns need to be zeroed out. This can be done by scanning the matrix to find zeroes and then marking the rows and columns that contain zeroes. Once this information is... | 7 | 0 | ['Array', 'Matrix', 'Java'] | 1 |
set-matrix-zeroes | C++/Java/Python3 Solution with Explanation | cjavapython3-solution-with-explanation-b-fthi | Intuition\nConstructive Problem with Observation\n\n# Complexity\n- Time complexity: O(N * N)\n\n- Space complexity: O(1)\n\n# Code\nC++ []\nclass Solution {\n | _limitlesspragma | NORMAL | 2024-04-26T13:37:24.785394+00:00 | 2024-04-26T13:37:49.372683+00:00 | 1,259 | false | # Intuition\nConstructive Problem with Observation\n\n# Complexity\n- Time complexity: O(N * N)\n\n- Space complexity: O(1)\n\n# Code\n```C++ []\nclass Solution {\n public:\n void setZeroes(vector<vector<int>>& matrix) {\n const int m = matrix.size();\n const int n = matrix[0].size();\n bool shouldFillFirstRo... | 7 | 0 | ['C++', 'Java', 'Python3'] | 0 |
set-matrix-zeroes | Best approach storing cordinates | best-approach-storing-cordinates-by-aksh-c9xb | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nstoring coordintes of cell in a different vector \ntraversing vector in s | Akshaypundir7579 | NORMAL | 2024-02-19T03:30:38.294662+00:00 | 2024-02-19T03:32:54.519783+00:00 | 835 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nstoring coordintes of cell in a different vector \ntraversing vector in seperate loop for making columns and rows zero\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(n)\n<!-- Ad... | 7 | 1 | ['C++'] | 5 |
set-matrix-zeroes | ✅ EASY C++ SOLUTION ☑️ | easy-c-solution-by-2005115-ialn | PLEASE UPVOTE MY SOLUTION IF YOU LIKE IT\n# CONNECT WITH ME\n### https://www.linkedin.com/in/pratay-nandy-9ba57b229/\n#### https://www.instagram.com/pratay_nand | 2005115 | NORMAL | 2024-02-11T17:40:44.849005+00:00 | 2024-02-11T17:40:44.849042+00:00 | 1,086 | false | # **PLEASE UPVOTE MY SOLUTION IF YOU LIKE IT**\n# **CONNECT WITH ME**\n### **[https://www.linkedin.com/in/pratay-nandy-9ba57b229/]()**\n#### **[https://www.instagram.com/_pratay_nandy_/]()**\n\n# Approach\nThis code implements a function `setZeroes` that modifies a given matrix in-place by setting entire rows and colum... | 7 | 0 | ['Array', 'Matrix', 'C++'] | 2 |
set-matrix-zeroes | SHORT, EASY, VERY CLEARLY EXPLAINED(step-by-step) 🔥🔥 || PYTHON 🐍 | short-easy-very-clearly-explainedstep-by-llys | SOLUTION\n\n\nclass Solution:\n def setZeroes(self, matrix):\n zero_rows, zero_cols = set(), set()\n for i in range(len(matrix)):\n for j in range | julialokot | NORMAL | 2023-03-12T17:48:54.156757+00:00 | 2023-03-12T17:48:54.156789+00:00 | 5,023 | false | # SOLUTION\n\n```\nclass Solution:\n def setZeroes(self, matrix):\n zero_rows, zero_cols = set(), set()\n for i in range(len(matrix)):\n for j in range(len(matrix[0])):\n if matrix[i][j] == 0:\n zero_rows.add(i)\n zero_cols.add(j)\n for i in zero_rows:\n ... | 7 | 0 | ['Python', 'Python3'] | 10 |
set-matrix-zeroes | O(N) space solution to O(1) space solution in C++ (Diagram included) | on-space-solution-to-o1-space-solution-i-kc6w | We can have a simple trivial solution by maintaining 2 arrays(1 for rows and 1 for columns)\n\nWe traverse the Matrix and if we find a 0 we mark the row and col | kumarsamaksha21 | NORMAL | 2021-07-29T09:57:56.722991+00:00 | 2021-07-31T07:10:17.738857+00:00 | 377 | false | **We can have a simple trivial solution by maintaining 2 arrays(1 for rows and 1 for columns)**\n\nWe traverse the Matrix and if we find a 0 we mark the row and column arrays as 1(in those index), then after traversing the whole array we again traverse the whole array to set the matrix as 0.\n\nTry dry running with thi... | 7 | 0 | ['C'] | 1 |
set-matrix-zeroes | Python Easy Inplace Solution | python-easy-inplace-solution-by-girikunc-r1p2 | Make first row and first column to mark which row and column should be zero before filling them out, then fill the matrix from right to left, and make the first | girikuncoro | NORMAL | 2016-02-02T12:11:12+00:00 | 2016-02-02T12:11:12+00:00 | 2,148 | false | Make first row and first column to mark which row and column should be zero before filling them out, then fill the matrix from right to left, and make the first row zero in the last if first row has any zero.\n\n def setZeroes(self, matrix):\n firstRowHasZero = not all(matrix[0])\n for i in range(1,len... | 7 | 2 | ['Python'] | 1 |
set-matrix-zeroes | In-place solution using constant space in C++, best submission | in-place-solution-using-constant-space-i-wdl8 | store the status of the line and column in the top-most or left-most\n- before that we should check the top-most row and left-most column and store their status | lhearen | NORMAL | 2016-07-04T06:28:34.166000+00:00 | 2016-07-04T06:28:34.166000+00:00 | 2,866 | false | - store the status of the line and column in the top-most or left-most\n- before that we should check the top-most row and left-most column and store their status first\n\n```\nclass Solution {\npublic:\n void setZeroes(vector<vector<int>>& matrix) \n {\n if(matrix.empty()) return ;\n int rowSize = ... | 7 | 3 | ['C'] | 4 |
set-matrix-zeroes | ✅✅Beats 100%🔥C++🔥Python|| 🚀🚀Super Simple and Efficient Solution🚀🚀||🔥Python🔥C++✅✅ | beats-100cpython-super-simple-and-effici-8fe5 | Complexity
Time complexity: O(mn)
Space complexity: O(1)
⬆️👇⬆️UPVOTE it⬆️👇⬆️Code⬆️👇⬆️UPVOTE it⬆️👇⬆️ | shobhit_yadav | NORMAL | 2025-01-28T03:34:08.652358+00:00 | 2025-01-28T03:34:08.652358+00:00 | 985 | false | # Complexity
- Time complexity: $$O(mn)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: $$O(1)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
⬆️👇⬆️UPVOTE it⬆️👇⬆️
# Code
```cpp []
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
const int m = matrix.... | 6 | 1 | ['Array', 'Hash Table', 'Matrix', 'C++', 'Python3'] | 1 |
set-matrix-zeroes | Optimal Approach with space complexity of O(1)..!! | optimal-approach-with-space-complexity-o-4q6n | Intuition\n Describe your first thoughts on how to solve this problem. \nFirstly i thought of brute and better approach\n1. first approach was like using near O | AkshajBansal | NORMAL | 2024-09-03T13:40:41.817128+00:00 | 2024-09-03T13:40:41.817163+00:00 | 13 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirstly i thought of brute and better approach\n1. first approach was like using near O(N^3)\n2. Second approach (better approach) consists of having two arrays for rows and columns. Then operating on them but it took space complexity of ... | 6 | 0 | ['C++'] | 0 |
set-matrix-zeroes | better version of brute force, 2 step solution | better-version-of-brute-force-2-step-sol-tmap | \n\n# Code\n\n// 2nd meathod, better than brute force\n// algo: we will create a row and col array and update its element as we get any 0s in our matrix\n\nclas | ujjuengineer | NORMAL | 2024-07-21T04:09:13.569015+00:00 | 2024-07-21T04:09:13.569056+00:00 | 533 | false | \n\n# Code\n```\n// 2nd meathod, better than brute force\n// algo: we will create a row and col array and update its element as we get any 0s in our matrix\n\nclass Solution {\npublic:\n void setZeroes(vector<vector<int>>& matrix) {\n int n = matrix.size();\n int m = matrix[0].size();\n vector<i... | 6 | 0 | ['C++'] | 0 |
set-matrix-zeroes | Easiest✅✅ || Beats 100%✅ || Java✅ | easiest-beats-100-java-by-wankhedeayush9-nu8s | Intuition:\n\nHere we are marking the ith index of row array i.e. row[i], and jth index of col array i.e. col[j] with 1. These marked indices of the two arrays | wankhedeayush90 | NORMAL | 2024-07-09T11:43:07.505545+00:00 | 2024-07-09T11:43:07.505575+00:00 | 1,502 | false | # Intuition:\n\nHere we are marking the ith index of row array i.e. row[i], and jth index of col array i.e. col[j] with 1. These marked indices of the two arrays, row and col will tell us for which rows and columns we need to change the values to 0. For any cell (i, j), if the row[i] or col[j] is marked with 1, we wil... | 6 | 0 | ['Matrix', 'C++', 'Java'] | 0 |
set-matrix-zeroes | Set Matrix Zeroes - Efficient Easy Java Solution Using Queue | set-matrix-zeroes-efficient-easy-java-so-u6bf | Intuition\nWhen I first approached this problem, I realized that if any element in the matrix is zero, we need to set the entire row and column of that element | kundan25 | NORMAL | 2024-06-26T16:58:58.016713+00:00 | 2024-06-26T16:58:58.016747+00:00 | 1,278 | false | # Intuition\nWhen I first approached this problem, I realized that if any element in the matrix is zero, we need to set the entire row and column of that element to zero. A straightforward way to track which rows and columns should be zeroed is by using a queue to store the coordinates of all zero elements.\n\n# Approa... | 6 | 0 | ['Java'] | 0 |
set-matrix-zeroes | Python In-Place Matrix Zeroing | python-in-place-matrix-zeroing-by-parado-1ak7 | Intuition\nThis problem involves setting the entire row and column to zero if any element in the matrix is zero. To efficiently achieve this, we can use two set | Paradox240 | NORMAL | 2024-01-09T21:45:08.575343+00:00 | 2024-01-09T21:45:08.575376+00:00 | 1,155 | false | # Intuition\nThis problem involves setting the entire row and column to zero if any element in the matrix is zero. To efficiently achieve this, we can use two sets (rows_to_zero and cols_to_zero) to keep track of which rows and columns need to be zeroed.\n\n# Approach\n1. Iterate through the matrix to identify the posi... | 6 | 0 | ['Python3'] | 1 |
set-matrix-zeroes | Kotlin easy solution | kotlin-easy-solution-by-gokuultra-pdhc | \n# Code\n\nclass Solution {\n fun setZeroes(matrix: Array<IntArray>): Unit {\n var firstRow = false\n var firstCol = false\n for (i in | GokuUltra | NORMAL | 2023-11-19T05:12:13.227872+00:00 | 2023-11-19T05:12:13.227898+00:00 | 390 | false | \n# Code\n```\nclass Solution {\n fun setZeroes(matrix: Array<IntArray>): Unit {\n var firstRow = false\n var firstCol = false\n for (i in matrix.indices)\n for (j in matrix[0].indices) {\n if (matrix[i][j] == 0) {\n if (i == 0) firstRow = true\n ... | 6 | 0 | ['Kotlin'] | 1 |
set-matrix-zeroes | C++ | Easy to Understand approach | c-easy-to-understand-approach-by-darkfir-2ak0 | Intuition\n Describe your first thoughts on how to solve this problem. \nIn this approach,I have initialsed 2 unordered maps(one for rows and one for columns) f | Darkfire5442 | NORMAL | 2023-07-24T00:49:19.293840+00:00 | 2023-07-24T00:49:19.293868+00:00 | 2,370 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn this approach,I have initialsed 2 unordered maps(one for rows and one for columns) for keeping the record weather a row or column contains a zero in it or not.For it I just traversed through the 2D array and once I get a zero, updated ... | 6 | 0 | ['C++'] | 1 |
set-matrix-zeroes | java || easiest solution | java-easiest-solution-by-ashutosh_369-tpwc | 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 | Ashutosh_369 | NORMAL | 2023-03-09T14:00:57.099488+00:00 | 2023-03-09T14:00:57.099521+00:00 | 1,233 | false | # 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 ( m*n )\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O( Math.max( n , m ))\n<!-- Add your space c... | 6 | 0 | ['Java'] | 0 |
set-matrix-zeroes | C++ || Easy to Understand || using two vectors | c-easy-to-understand-using-two-vectors-b-7qer | \n# Code\n\nclass Solution {\npublic:\n void setZeroes(vector<vector<int>>& arr) {\n \n int r = arr.size();\n int c = arr[0].size();\n\n | Parth_the_error | NORMAL | 2022-12-30T10:13:11.871734+00:00 | 2022-12-30T10:13:11.871763+00:00 | 1,447 | false | \n# Code\n```\nclass Solution {\npublic:\n void setZeroes(vector<vector<int>>& arr) {\n \n int r = arr.size();\n int c = arr[0].size();\n\n vector<int> xr,yc;\n\n\n for(int i=0;i<r;i++)\n {\n for(int j =0;j<c;j++)\n {\n if(arr[i][j] == 0)... | 6 | 0 | ['C++'] | 0 |
set-matrix-zeroes | My very simple Python 🐍 solution O(n) time O(1) space | my-very-simple-python-solution-on-time-o-ngnx | \nclass Solution:\n def setZeroes(self, matrix: List[List[int]]) -> None:\n\n def changeRC(r,c):\n \n for col in range(len(matri | injysarhan | NORMAL | 2020-10-31T18:11:29.099563+00:00 | 2020-10-31T18:11:29.099611+00:00 | 1,562 | false | ```\nclass Solution:\n def setZeroes(self, matrix: List[List[int]]) -> None:\n\n def changeRC(r,c):\n \n for col in range(len(matrix[0])):\n matrix[r][col]=\'X\' if matrix[r][col]!=0 else 0\n for row in range(len(matrix)):\n matrix[row][c]=\'X\' i... | 6 | 1 | ['Python', 'Python3'] | 2 |
set-matrix-zeroes | C++: Easy, fast and short(9 lines) solution | c-easy-fast-and-short9-lines-solution-by-hg0t | This solution is very short, simple.\nThe process is divided into three parts.\n\n1. Traverse the matrix and finds 0. If it finds 0, it insert row and column in | o-oppang | NORMAL | 2020-09-10T16:00:50.486325+00:00 | 2020-09-10T16:02:17.767350+00:00 | 964 | false | This solution is very short, simple.\nThe process is divided into three parts.\n\n1. Traverse the matrix and finds 0. If it finds 0, it insert row and column in set `rows`, `cols`.\n2. Iterate through the row(`rows`) and fill in zeros.\n3. Iterate through the column(`cols`), fill in zeros.\n\n```\n// origin - https://g... | 6 | 0 | ['C', 'C++'] | 1 |
set-matrix-zeroes | [Java] O(1) Space with Explanation | java-o1-space-with-explanation-by-haimei-cvyx | Thinking Processing: \n1. First, Let\'s come out O(m+n) Space solution, which is very directly. We need two arrays to store rows and columns status. It looks li | haimei2 | NORMAL | 2020-02-27T23:54:23.324676+00:00 | 2020-02-27T23:57:09.476099+00:00 | 263 | false | Thinking Processing: \n1. First, Let\'s come out O(m+n) Space solution, which is very directly. We need two arrays to store rows and columns status. It looks like a head of a line to check its line\'s status. When we loop each point, we just need to update its two headers\' status. If current point\'s value is 0, we se... | 6 | 0 | [] | 1 |
set-matrix-zeroes | JAVA - Easy and Short Solution with EXPLANATION! :) | java-easy-and-short-solution-with-explan-o7hr | We plan to use the firstRow and firstCol of the matrix to store the state of entire row and matrix. \n\nBUT\n\nDoing this, we might loose the state of the first | anubhavjindal | NORMAL | 2020-01-26T18:39:57.780027+00:00 | 2020-01-27T00:44:23.305659+00:00 | 296 | false | We plan to use the firstRow and firstCol of the matrix to store the state of entire row and matrix. \n\n**BUT**\n\nDoing this, we might loose the state of the firstRow and firstCol. So we use two variables isFirstRowZero and isFirstColZero to store the state of irstRow and firstCol and later use it to restore them to t... | 6 | 0 | [] | 0 |
set-matrix-zeroes | JAVA SOLUTION, O(1) SPACE, BEATS 100% IN TIME, WITH EXPLANATION. | java-solution-o1-space-beats-100-in-time-m6va | We can reduce the space to O(1) by using the first row as a replacement for the row array and the first column as a replacement for the column array.This works | hans19 | NORMAL | 2019-08-14T14:51:25.292278+00:00 | 2019-08-17T13:26:45.177892+00:00 | 773 | false | We can reduce the space to O(1) by using the first row as a replacement for the row array and the first column as a replacement for the column array.This works as follows:\n\n1. Check if the first row and first column have any zeroes, and set variables rowHasZero and columnHasZero.(We\'ll nullify the first row and firs... | 6 | 2 | ['Java'] | 0 |
set-matrix-zeroes | My 4-line code for Python | my-4-line-code-for-python-by-dr_sean-wdv6 | \nclass Solution(object):\n def setZeroes(self, matrix):\n points = [[i,j] for i in range(len(matrix)) for j in range(len(matrix[i])) if matrix[i][j] | dr_sean | NORMAL | 2019-01-05T08:17:42.920026+00:00 | 2019-01-05T08:17:42.920076+00:00 | 504 | false | ```\nclass Solution(object):\n def setZeroes(self, matrix):\n points = [[i,j] for i in range(len(matrix)) for j in range(len(matrix[i])) if matrix[i][j] == 0]\n for i, a in enumerate(points):\n matrix[a[0]] = [0 for k in range(len(matrix[0]))]\n for k in range(len(matrix)): matrix... | 6 | 0 | [] | 1 |
set-matrix-zeroes | Java 2 solutions: space O(1) and O(m+n), with explaination | java-2-solutions-space-o1-and-omn-with-e-p6gd | space O(1), time O(mn)\nUse the top row and the left column to record which rows and columns need to be set 0. topZero and leftZero to record whether we need to | maddetective | NORMAL | 2016-10-11T22:07:18.526000+00:00 | 2016-10-11T22:07:18.526000+00:00 | 1,206 | false | space O(1), time O(mn)\nUse the top row and the left column to record which rows and columns need to be set 0. topZero and leftZero to record whether we need to set the top row and the left column to zero before finished.\n```\n public void setZeroes(int[][] matrix) {\n if(matrix==null) return;\n fina... | 6 | 1 | [] | 0 |
set-matrix-zeroes | Set Matrix Zeroes - Second Approach | set-matrix-zeroes-easy-explanation-with-wt80x | Understanding the QuestionThe question asks to set those rows and columns as zero which have one element as zero.ApproachWe will be discussing multiple approach | ak9807 | NORMAL | 2025-02-03T12:57:13.863183+00:00 | 2025-02-04T03:57:23.373850+00:00 | 601 | false | # Understanding the Question
<!-- Describe your first thoughts on how to solve this problem. -->
The question asks to set those rows and columns as zero which have one element as zero.
---
# Approach
<!-- Describe your approach to solving the problem. -->
We will be discussing multiple approaches, You can check out t... | 5 | 0 | ['Array', 'Hash Table', 'Matrix', 'Java'] | 1 |
set-matrix-zeroes | Easy solution in python | easy-solution-in-python-by-sriharshini_0-e63z | Code | sriharshini_02 | NORMAL | 2025-01-21T17:53:55.757765+00:00 | 2025-01-21T17:53:55.757765+00:00 | 525 | false |
# Code
```python3 []
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
r,c=len(matrix),len(matrix[0])
rowstofill, colstofill = set(),set()
for i in range(r):
for j in ran... | 5 | 0 | ['Python3'] | 0 |
set-matrix-zeroes | Efficient In-Place Matrix Zeroing: In O(m*n) | efficient-in-place-matrix-zeroing-in-omn-gci3 | Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to set the entire row and column to zero if any element in the matrix is ze | Ashu3213 | NORMAL | 2024-08-06T17:57:24.125898+00:00 | 2024-08-06T17:57:24.125926+00:00 | 1,190 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to set the entire row and column to zero if any element in the matrix is zero.\n- This must be done **in-place** without using an extra matrix.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Use an e... | 5 | 0 | ['Array', 'Matrix', 'Python3'] | 0 |
set-matrix-zeroes | Most optimal JAVA solution with explained approach || TC->O(m * n) & SC->O(1) | most-optimal-java-solution-with-explaine-pex5 | Intuition\n\nThe problem requires setting entire rows and columns to zero if any element in that row or column is zero. A naive approach would involve using ext | mohitjaisal | NORMAL | 2024-07-19T18:17:39.397835+00:00 | 2024-07-21T14:44:29.538392+00:00 | 218 | false | # Intuition\n\nThe problem requires setting entire rows and columns to zero if any element in that row or column is zero. A naive approach would involve using extra space to track the rows and columns to be zeroed, but we can optimize this by using the first row and the first column of the matrix itself as markers. Thi... | 5 | 0 | ['Array', 'Hash Table', 'Matrix', 'Java'] | 1 |
set-matrix-zeroes | Java Solution | Easy Solution! Easy he bhai | Step follow karloo ✅ ✅ | java-solution-easy-solution-easy-he-bhai-exeh | Certainly! Here is a detailed explanation of the code that sets matrix rows and columns to zero if an element is zero:\n\njava\nclass Solution {\n public voi | kaushikwagh21 | NORMAL | 2024-07-02T07:48:49.521338+00:00 | 2024-07-02T07:48:49.521361+00:00 | 1,988 | false | Certainly! Here is a detailed explanation of the code that sets matrix rows and columns to zero if an element is zero:\n\n```java\nclass Solution {\n public void setZeroes(int[][] matrix) {\n int rows = matrix.length;\n int cols = matrix[0].length;\n boolean[][] isOriginalZero = new boolean[rows... | 5 | 0 | ['Java'] | 1 |
valid-phone-numbers | (BASH) simple regular expression with explanation | bash-simple-regular-expression-with-expl-dpmy | \n\ngrep -e "^[0-9]\\{3\\}\\-[0-9]\\{3\\}\\-[0-9]\\{4\\}$" -e "^([0-9]\\{3\\}) [0-9]\\{3\\}\\-[0-9]\\{4\\}$" file.txt\n\nthis is a grep command accepting two re | savas_karanli | NORMAL | 2022-07-08T09:34:24.812459+00:00 | 2022-07-25T12:41:19.699934+00:00 | 24,434 | false | \n```\ngrep -e "^[0-9]\\{3\\}\\-[0-9]\\{3\\}\\-[0-9]\\{4\\}$" -e "^([0-9]\\{3\\}) [0-9]\\{3\\}\\-[0-9]\\{4\\}$" file.txt\n```\nthis is a grep command accepting two regular expressions\n1 .```^[0-9]\\{3\\}\\-[0-9]\\{3\\}\\-[0-9]\\{4\\}$```\n2 .```^([0-9]\\{3\\}) [0-9]\\{3\\}\\-[0-9]\\{4\\}$```\n\nThe construction is as ... | 77 | 0 | [] | 17 |
valid-phone-numbers | Simple Bash Solutions Explained || grep & awk | simple-bash-solutions-explained-grep-awk-yewv | I have compiled a list of possible solutions using grep or awk as well as explaining the tools/syntax used. Hope it helps!\n\nCommon syntax explained:\n ^ Start | NathanPaceydev | NORMAL | 2022-06-27T16:44:32.028453+00:00 | 2022-06-27T16:45:04.369029+00:00 | 6,952 | false | I have compiled a list of possible solutions using grep or awk as well as explaining the tools/syntax used. Hope it helps!\n\n**Common syntax explained:**\n* `^` Start of a line (not just within a line, ex `112-122-2313` **not** `022121-112-2313`)\n* `[0-9]` regex expression to represent any digit between 0 and 9.\n* `... | 34 | 0 | [] | 3 |
valid-phone-numbers | Small regex, 100% faster | small-regex-100-faster-by-g2codes-3dok | \negrep "^(\\([0-9]{3}\\) |[0-9]{3}\\-)[0-9]{3}\\-[0-9]{4}$" file.txt\n | g2codes | NORMAL | 2020-01-28T05:35:49.366590+00:00 | 2020-01-28T05:35:49.366638+00:00 | 7,745 | false | ```\negrep "^(\\([0-9]{3}\\) |[0-9]{3}\\-)[0-9]{3}\\-[0-9]{4}$" file.txt\n``` | 24 | 0 | [] | 0 |
valid-phone-numbers | Valid Numbers | valid-numbers-by-arunadang-c16r | ```\nPlease find the solution : \ngrep -P \'^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$\' file.txt\n\nExplaination : \ngrep -P \u2018^()\n\nWhat in these parentheses shoul | arunadang | NORMAL | 2020-05-30T22:25:26.323023+00:00 | 2020-05-30T22:25:26.323057+00:00 | 4,374 | false | ```\nPlease find the solution : \ngrep -P \'^(\\d{3}-|\\(\\d{3}\\) )\\d{3}-\\d{4}$\' file.txt\n\nExplaination : \ngrep -P \u2018^()\n\nWhat in these parentheses should come in the beginning.\ngrep -P \u2018^(\\d{3}-\n\\d{3} - means 3 digits should come in these parenthesis.\nGrep -P \u2018^(\\d{3}-|\\(\\d{3}\\) )\u201... | 19 | 1 | [] | 2 |
valid-phone-numbers | [BASH] - Easy One Liner | bash-easy-one-liner-by-justcodingandcars-9nxa | \ngrep -P \'^(\\d{3}-\\d{3}-\\d{4}|\\(\\d{3}\\) \\d{3}-\\d{4})$\' file.txt\n | justcodingandcars | NORMAL | 2020-09-12T18:14:18.094004+00:00 | 2020-09-12T18:14:27.652792+00:00 | 5,892 | false | ```\ngrep -P \'^(\\d{3}-\\d{3}-\\d{4}|\\(\\d{3}\\) \\d{3}-\\d{4})$\' file.txt\n``` | 15 | 1 | [] | 2 |
valid-phone-numbers | [Bash] grep BRE | ERE | PCRE | bash-grep-bre-ere-pcre-by-ye15-zpgs | Regular exrepssion (regex) has at least 3 official flavors: \n1) basic regex (BRE)\n2) extended regex (ERE)\n3) perl-compatible regex (PCRE)\n\nThey share great | ye15 | NORMAL | 2020-04-01T04:32:26.530981+00:00 | 2020-04-01T04:32:26.531017+00:00 | 1,504 | false | Regular exrepssion (regex) has at least 3 official flavors: \n1) basic regex (BRE)\n2) extended regex (ERE)\n3) perl-compatible regex (PCRE)\n\nThey share great resemblance and yet differ in details. Below implementation uses `grep` as an example which by default applies BRE. `-E` turns on ERE and `-P` turns on PCRE. \... | 14 | 0 | [] | 1 |
valid-phone-numbers | Simple: the interviewer who asked this question should be disqualified from interview panel. | simple-the-interviewer-who-asked-this-qu-9qhr | Simple: the interviewer who asked this question should be disqualified from interview panel. | _patrick_ | NORMAL | 2022-08-01T02:38:00.432958+00:00 | 2022-08-01T02:38:00.433003+00:00 | 1,932 | false | Simple: the interviewer who asked this question should be disqualified from interview panel. | 8 | 0 | [] | 2 |
valid-phone-numbers | Two one-line solution: grep or sed UNIX solution | two-one-line-solution-grep-or-sed-unix-s-pztt | grep (find the numbers which are matched, which is straight-forward):\n\ngrep \'^\\(([0-9]\\{3\\}) \\|[0-9]\\{3\\}-\\)[0-9]\\{3\\}-[0-9]\\{4\\}$\' file.txt\n\ns | yunfrank427 | NORMAL | 2021-09-21T22:24:15.047163+00:00 | 2021-09-22T01:49:28.779103+00:00 | 2,088 | false | grep (find the numbers which are matched, which is straight-forward):\n```\ngrep \'^\\(([0-9]\\{3\\}) \\|[0-9]\\{3\\}-\\)[0-9]\\{3\\}-[0-9]\\{4\\}$\' file.txt\n```\nsed (delete the numbers which are mismatched):\n```\nsed \'/^\\(([0-9]\\{3\\}) \\|[0-9]\\{3\\}-\\)[0-9]\\{3\\}-[0-9]\\{4\\}$/!d\' file.txt\n```\n | 8 | 0 | [] | 0 |
valid-phone-numbers | This solution is faster than 100.00% of Bash online submissions for Valid Phone Numbers | this-solution-is-faster-than-10000-of-ba-h2v4 | \negrep \'^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\\([0-9]{3}\\)\\s[0-9]{3}-[0-9]{4}$\' file.txt\n | salimhussain | NORMAL | 2021-09-20T08:58:54.200208+00:00 | 2021-09-20T08:58:54.200253+00:00 | 3,417 | false | ```\negrep \'^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\\([0-9]{3}\\)\\s[0-9]{3}-[0-9]{4}$\' file.txt\n``` | 8 | 0 | [] | 1 |
valid-phone-numbers | GREP || Easy || Pattern matching | grep-easy-pattern-matching-by-sharan_k-wdqo | \n# GREP stands for Global search for Regular Expression and Print out\n# grep [options] pattern [files]\n# This is the syntax.\n\ngrep -Po \'^(\\(\\d{3}\\) |\\ | Sharan_k | NORMAL | 2022-01-31T08:35:46.092232+00:00 | 2022-01-31T08:35:46.092279+00:00 | 2,157 | false | ```\n# GREP stands for Global search for Regular Expression and Print out\n# grep [options] pattern [files]\n# This is the syntax.\n\ngrep -Po \'^(\\(\\d{3}\\) |\\d{3}-)\\d{3}-\\d{4}$\' file.txt\n```\nPlease **UpVote**, if you understood the code. | 7 | 1 | [] | 1 |
valid-phone-numbers | Solution: 0ms - 100% - 3.1MB | solution-0ms-100-31mb-by-iamsureshraju-jtsc | bash\n# Runtime: 0 ms, faster than 100.00% of Bash online submissions for Valid Phone Numbers.\n# Memory Usage: 3.1 MB, less than 87.19% of Bash online submissi | iamsureshraju | NORMAL | 2020-09-07T15:40:17.281930+00:00 | 2020-09-07T15:40:17.281973+00:00 | 1,970 | false | ```bash\n# Runtime: 0 ms, faster than 100.00% of Bash online submissions for Valid Phone Numbers.\n# Memory Usage: 3.1 MB, less than 87.19% of Bash online submissions for Valid Phone Numbers.\negrep "^\\([0-9]{3}\\) [0-9]{3}\\-[0-9]{4}$|^[0-9]{3}\\-[0-9]{3}\\-[0-9]{4}$" file.txt\n``` | 7 | 1 | [] | 1 |
valid-phone-numbers | Why is this failing when it works locally? | why-is-this-failing-when-it-works-locall-zq0u | \ngrep -E -o "\\((\\d{3})) \\d{3}-\\d{4}|\\d{3}-\\d{3}-\\d{4}" file.txt\n\n\npasses 21/26 test cases but not \n123-456-7891 ? | leeroywking | NORMAL | 2019-09-13T01:23:42.286457+00:00 | 2019-09-13T01:23:42.286496+00:00 | 823 | false | ```\ngrep -E -o "\\((\\d{3})) \\d{3}-\\d{4}|\\d{3}-\\d{3}-\\d{4}" file.txt\n```\n\npasses 21/26 test cases but not \n123-456-7891 ? | 7 | 0 | [] | 2 |
valid-phone-numbers | 193: Solution step by step explanation | 193-solution-step-by-step-explanation-by-gz6x | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nExplanation:\n\n- grep command is used to search for a pattern in a file | Marlen09 | NORMAL | 2023-02-22T16:31:14.764184+00:00 | 2023-02-22T16:31:14.764218+00:00 | 8,535 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nExplanation:\n\n- grep command is used to search for a pattern in a file or files.\n- -E option is used to enable extended regular expressions.\n```\n\'^(\\([0-9]{3}\\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$\' \n``` \nis the regular ... | 6 | 0 | ['Shell', 'Bash'] | 4 |
valid-phone-numbers | SIMPLEST REGEX | simplest-regex-by-aadi_01-0ocr | \n# Read from the file file.txt and output all valid phone numbers to stdout.\negrep \'^([0-9]{3}-|\\([0-9]{3}\\) )[0-9]{3}-[0-9]{4}$\' file.txt\n\n# ^ -> Sta | aadi_01 | NORMAL | 2021-09-17T05:33:41.192504+00:00 | 2021-09-17T05:33:54.739338+00:00 | 1,494 | false | ```\n# Read from the file file.txt and output all valid phone numbers to stdout.\negrep \'^([0-9]{3}-|\\([0-9]{3}\\) )[0-9]{3}-[0-9]{4}$\' file.txt\n\n# ^ -> Start of the Line.\n# $ -> End of the Line.\n# ([0-9]) -> Value Range can be between 0 to 9.\n# ([0-9]{3}) -> Value Range can be between 0 to 9 and repeat... | 6 | 0 | [] | 0 |
valid-phone-numbers | Bash || grep || Very simple solution | bash-grep-very-simple-solution-by-meurud-411e | \n# Code\n\n# Read from the file file.txt and output all valid phone numbers to stdout.\ngrep \'^\\([0-9]\\{3\\}-\\|([0-9]\\{3\\}) \\)[0-9]\\{3\\}-[0-9]\\{4\\}$ | meurudesu | NORMAL | 2024-02-07T14:25:36.732499+00:00 | 2024-02-07T14:25:36.732521+00:00 | 3,227 | false | \n# Code\n```\n# Read from the file file.txt and output all valid phone numbers to stdout.\ngrep \'^\\([0-9]\\{3\\}-\\|([0-9]\\{3\\}) \\)[0-9]\\{3\\}-[0-9]\\{4\\}$\' file.txt\n``` | 5 | 0 | ['Shell', 'Bash'] | 1 |
valid-phone-numbers | grep -P solution | grep-p-solution-by-jordanbanana-uoc9 | Intro\n\nThis is a simple Perl RegEx solution. It requires no other commands. If additional challenges were added, this may become a bit too noisy as a one line | jordanbanana | NORMAL | 2021-04-11T21:04:39.434178+00:00 | 2021-04-11T21:06:21.077006+00:00 | 797 | false | ## Intro\n\nThis is a simple Perl RegEx solution. It requires no other commands. If additional challenges were added, this may become a bit too noisy as a one liner.\n\n## Solution\n\n```\ngrep -P \'^(\\(\\d{3}\\) |\\d{3}-)\\d{3}\\-\\d{4}$\' file.txt \n```\n\n## Breakdown\n\nWe start off with the start of line (`^`) ch... | 5 | 0 | [] | 0 |
valid-phone-numbers | Short solution using grep (with explanation) | short-solution-using-grep-with-explanati-rwxu | One-liner solution\n\ngrep \'^\\([0-9]\\{3\\}-\\|([0-9]\\{3\\}) \\)[0-9]\\{3\\}-[0-9]\\{4\\}$\' file.txt\n\n\n^ symbol for must begin with, xxx- OR \'(xxx) \' q | cannotsleep | NORMAL | 2021-03-22T11:35:42.954484+00:00 | 2021-03-22T11:35:42.954514+00:00 | 1,018 | false | **One-liner solution**\n```\ngrep \'^\\([0-9]\\{3\\}-\\|([0-9]\\{3\\}) \\)[0-9]\\{3\\}-[0-9]\\{4\\}$\' file.txt\n```\n\n**^ symbol for must begin with, xxx- OR \'(xxx) \' quoted for space.**\n```\n^\\([0-9]\\{3\\}-\\|([0-9]\\{3\\}) \\)\n```\n\n**$ symbol for must end with xxx-xxxx**\n```\n[0-9]\\{3\\}-[0-9]\\{4\\}$\n``... | 5 | 0 | [] | 0 |
valid-phone-numbers | Grep only: 0ms. Extended Regex and Regex. | grep-only-0ms-extended-regex-and-regex-b-c1aa | BASH\n# https://leetcode.com/problems/valid-phone-numbers/submissions/\n# Runtime: 0 ms, faster than 100.00% of Bash online submissions for Valid Phone Numbers. | user9697n | NORMAL | 2019-10-26T17:30:50.569178+00:00 | 2019-10-26T17:30:50.569227+00:00 | 1,198 | false | ```BASH\n# https://leetcode.com/problems/valid-phone-numbers/submissions/\n# Runtime: 0 ms, faster than 100.00% of Bash online submissions for Valid Phone Numbers.\n# Memory Usage: 3.1 MB, less than 96.43% of Bash online submissions for Valid Phone Numbers.\ngrep -E \'^(\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}|[0-9]{3}-[0-9]{3... | 5 | 0 | [] | 0 |
valid-phone-numbers | grep solution beats 100% | grep-solution-beats-100-by-bbrotherseu-wptn | A easy way to get it by using grep\n\n-E means use regular expression\n\n\ncat file.txt | grep -E \'^\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}$|^[0-9]{3}-[0-9]{3}-[0-9]{ | bbrotherseu | NORMAL | 2019-04-17T03:22:07.940307+00:00 | 2019-04-17T03:22:07.940351+00:00 | 3,215 | false | A easy way to get it by using grep\n\n-E means use regular expression\n\n```\ncat file.txt | grep -E \'^\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}$|^[0-9]{3}-[0-9]{3}-[0-9]{4}$\'\n``` | 5 | 1 | [] | 3 |
valid-phone-numbers | Simple solution using awk | simple-solution-using-awk-by-yangshun-4y97 | \nawk '/^(\\([0-9]{3}\\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$/' file.txt\n | yangshun | NORMAL | 2017-09-14T23:44:24.029000+00:00 | 2017-09-14T23:44:24.029000+00:00 | 2,590 | false | ```\nawk '/^(\\([0-9]{3}\\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$/' file.txt\n``` | 5 | 0 | [] | 0 |
valid-phone-numbers | One Line | Bash Solution | one-line-bash-solution-by-raghavdabra-vju0 | grep -P \'(^\\(\\d{3}\\) \\d{3}\\-\\d{4}$)|(^\\d{3}\\-\\d{3}\\-\\d{4}$)\' file.txt | raghavdabra | NORMAL | 2022-10-19T13:00:47.146964+00:00 | 2022-10-19T13:00:47.147004+00:00 | 3,463 | false | `grep -P \'(^\\(\\d{3}\\) \\d{3}\\-\\d{4}$)|(^\\d{3}\\-\\d{3}\\-\\d{4}$)\' file.txt` | 4 | 0 | [] | 0 |
valid-phone-numbers | grep -E | grep-e-by-trpaslik-j2gb | here is my code.\nI use grep with extended RE.\n[0-9]{3} means digit exactly 3 times\n^ is start of line\n$ is end of line\n| is alternative\n() are for groupin | trpaslik | NORMAL | 2022-05-13T15:59:37.896046+00:00 | 2022-05-13T15:59:37.896086+00:00 | 747 | false | here is my code.\nI use grep with extended RE.\n`[0-9]{3}` means digit exactly 3 times\n`^` is start of line\n`$` is end of line\n`|` is alternative\n`()` are for grouping\n`\\(` `\\)` are literally parentasis (not interpered as grouping)\n```bash\ngrep -E \'^([0-9]{3}-[0-9]{3}-[0-9]{4}$)|^(\\([0-9]{3}\\) )[0-9]{3}-[0-... | 4 | 0 | [] | 1 |
valid-phone-numbers | awk and regex | awk-and-regex-by-yellow_jack-4kr8 | \nawk \'/^([0-9]{3}-[0-9]{3}-[0-9]{4}|\\([0-9]{3}\\) [0-9]{3}-[0-9]{4})$/\' < file.txt\n | yellow_jack | NORMAL | 2021-06-22T21:37:56.360500+00:00 | 2021-06-22T21:37:56.360545+00:00 | 545 | false | ```\nawk \'/^([0-9]{3}-[0-9]{3}-[0-9]{4}|\\([0-9]{3}\\) [0-9]{3}-[0-9]{4})$/\' < file.txt\n``` | 4 | 0 | [] | 1 |
valid-phone-numbers | [GREP] Regex | grep-regex-by-yurokusa-rhon | \ngrep -o \'^\\([0-9]\\{3\\}-\\|([0-9]\\{3\\}) \\)[0-9]\\{3\\}-[0-9]\\{4\\}$\' file.txt\n | yurokusa | NORMAL | 2021-01-28T03:30:21.853949+00:00 | 2021-01-28T03:30:21.854012+00:00 | 1,308 | false | ```\ngrep -o \'^\\([0-9]\\{3\\}-\\|([0-9]\\{3\\}) \\)[0-9]\\{3\\}-[0-9]\\{4\\}$\' file.txt\n``` | 4 | 0 | [] | 1 |
valid-phone-numbers | One line grep -E | one-line-grep-e-by-twl007-0jf9 | \ngrep -E -e "^(\\([0-9]{3}\\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$"\n | twl007 | NORMAL | 2018-11-29T18:49:54.238879+00:00 | 2018-11-29T18:49:54.238932+00:00 | 2,219 | false | ```\ngrep -E -e "^(\\([0-9]{3}\\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$"\n``` | 4 | 0 | [] | 2 |
valid-phone-numbers | My "grep -E" solution | my-grep-e-solution-by-tryharder-k9zq | # Read from the file file.txt and output all valid phone numbers to stdout.\n \n # use grep -P\n grep -P '^\\([0-9]{3}\\)\\s[0-9]{3}-[0-9]{4}$|^[0-9]{3 | tryharder | NORMAL | 2015-12-03T05:08:11+00:00 | 2015-12-03T05:08:11+00:00 | 3,910 | false | # Read from the file file.txt and output all valid phone numbers to stdout.\n \n # use grep -P\n grep -P '^\\([0-9]{3}\\)\\s[0-9]{3}-[0-9]{4}$|^[0-9]{3}-[0-9]{3}-[0-9]{4}$' file.txt | 4 | 0 | [] | 1 |
valid-phone-numbers | valid phone numbers | valid-phone-numbers-by-jeetkarena3-zzxt | IntuitionTo extract valid phone numbers from a file, we can use regular expressions to identify the correct patterns.ApproachWe will use thegrepcommand to searc | jeetkarena3 | NORMAL | 2025-02-16T16:42:48.948132+00:00 | 2025-02-18T09:37:24.700395+00:00 | 1,137 | false | # Intuition
To extract valid phone numbers from a file, we can use regular expressions to identify the correct patterns.
# Approach
We will use the `grep` command to search for lines in the file that match the desired phone number patterns:
1. Pattern `xxx-xxx-xxxx` (where `x` is a digit).
2. Pattern `(xxx) xxx-xxxx... | 3 | 0 | ['Shell', 'Bash'] | 1 |
valid-phone-numbers | Just bash regex | just-bash-regex-by-district_12-4ufp | Using bash regex only.\nRead a file line by line, first check the simple expression in case most of the phone numbers will be in a simpler format.\n\n# Code\n\n | District_12 | NORMAL | 2022-12-26T10:36:06.323280+00:00 | 2022-12-26T10:36:27.546168+00:00 | 1,107 | false | Using bash regex only.\nRead a file line by line, first check the simple expression in case most of the phone numbers will be in a simpler format.\n\n# Code\n```\n# Read from the file file.txt and output all valid phone numbers to stdout.\nwhile read line; do if [[ $line =~ (^[0-9]{3}-[0-9]{3}-[0-9]{4}$) || $line =~ (^... | 3 | 0 | ['Bash'] | 0 |
valid-phone-numbers | Simple sol using grep | simple-sol-using-grep-by-shellpy03-k31r | \ngrep -P \'^(\\(\\d{3}\\) |\\d{3}-)\\d{3}-\\d{4}$\' file.txt\n | shellpy03 | NORMAL | 2022-09-11T13:06:41.115366+00:00 | 2022-09-11T13:06:41.115411+00:00 | 296 | false | ```\ngrep -P \'^(\\(\\d{3}\\) |\\d{3}-)\\d{3}-\\d{4}$\' file.txt\n``` | 3 | 2 | [] | 0 |
valid-phone-numbers | Proper grep solution with standard PCRE regex | proper-grep-solution-with-standard-pcre-hw0uc | Easy to understand what is going on.\n\nGrep flags used:\n \t-o for printing results in newline\n \t-P to enable standard PCRE regex matching followed by the re | 1_DC | NORMAL | 2022-07-09T16:02:56.585554+00:00 | 2022-07-09T16:02:56.585600+00:00 | 1,030 | false | Easy to understand what is going on.\n\nGrep flags used:\n* \t```-o``` for printing results in newline\n* \t```-P``` to enable standard PCRE regex matching followed by the regex expression.\n\n```\ngrep -o -P \'^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}$\' file.txt\n``` | 3 | 0 | [] | 0 |
valid-phone-numbers | grep -E Solution with regex | grep-e-solution-with-regex-by-anshrathod-0as5 | grep -E Solution with regex\n--- \n\ngrep -E "^(\\([0-9]{3}\\) |[0-9]{3}\\-)[0-9]{3}\\-[0-9]{4}$" file.txt\n | anshrathod | NORMAL | 2022-04-01T12:10:05.198956+00:00 | 2022-04-01T12:10:05.198998+00:00 | 1,089 | false | ## grep -E Solution with regex\n--- \n```\ngrep -E "^(\\([0-9]{3}\\) |[0-9]{3}\\-)[0-9]{3}\\-[0-9]{4}$" file.txt\n``` | 3 | 0 | [] | 1 |
valid-phone-numbers | 0ms solution. 100% faster! | 0ms-solution-100-faster-by-bearsuper-ggfq | \ngrep -Po \'^(\\(\\d{3}\\) |\\d{3}-)\\d{3}-\\d{4}$\' file.txt\n | bearsuper | NORMAL | 2021-11-30T15:52:49.615495+00:00 | 2021-11-30T15:56:47.208437+00:00 | 1,662 | false | ```\ngrep -Po \'^(\\(\\d{3}\\) |\\d{3}-)\\d{3}-\\d{4}$\' file.txt\n``` | 3 | 0 | [] | 0 |
valid-phone-numbers | sed & regex | 1-liner | Explanation | sed-regex-1-liner-explanation-by-idontkn-r56v | Explanation\nThe idea is to local certain pattern and print them out. regex is the perfect choice to locate the pattern. sed can be a easy to use helper here.\n | idontknoooo | NORMAL | 2021-05-08T03:52:20.507372+00:00 | 2021-05-08T03:52:20.507403+00:00 | 793 | false | ## Explanation\nThe idea is to local certain pattern and print them out. `regex` is the perfect choice to locate the pattern. `sed` can be a easy to use helper here.\n\nThere are 2 patterns needed:\n- For `123-456-7890`, we can use pattern like this\n\t- `^[0-9]{3}-[0-9]{3}-[0-9]{4}$`\n\t- Meaning starting with 3 digit... | 3 | 0 | [] | 0 |
valid-phone-numbers | grep extended regex | grep-extended-regex-by-dfsbfs-31ps | ```\ngrep -E "^[0-9]{3}[-]{1}[0-9]{3}[-]{1}[0-9]{4}$|^\([0-9]{3}\)\ {1}[0-9]{3}[-]{1}[0-9]{4}$" file.txt | dfsbfs | NORMAL | 2020-12-27T15:58:49.978300+00:00 | 2020-12-27T15:58:49.978346+00:00 | 580 | false | ```\ngrep -E "^[0-9]{3}[-]{1}[0-9]{3}[-]{1}[0-9]{4}$|^\\([0-9]{3}\\)\\ {1}[0-9]{3}[-]{1}[0-9]{4}$" file.txt | 3 | 0 | [] | 1 |
valid-phone-numbers | Here's the 3 line Code 🔥 | heres-the-3-line-code-by-starkbbk-qffg | Code | starkbbk | NORMAL | 2025-03-19T18:37:31.680689+00:00 | 2025-03-19T18:37:31.680689+00:00 | 733 | false |
# Code
```bash []
#!/bin/bash
# File containing the phone numbers
file="file.txt"
# Regular expression to match valid phone numbers
regex="^([0-9]{3}-[0-9]{3}-[0-9]{4})$|^(\([0-9]{3}\) [0-9]{3}-[0-9]{4})$"
# Extract valid phone numbers using grep
grep -E "$regex" "$file"
``` | 2 | 0 | ['Bash'] | 0 |
valid-phone-numbers | im proud of python | im-proud-of-python-by-doggz-7g71 | As a python dev, I feel so proud to solve this problem via python.Code | doggz | NORMAL | 2025-03-09T12:44:42.517979+00:00 | 2025-03-09T12:44:42.517979+00:00 | 744 | false | As a python dev, I feel so proud to solve this problem via python.
# Code
```bash []
# Read from the file file.txt and output all valid phone numbers to stdout.
python3 -c "
import re
with open('file.txt', 'r') as file:
for line in file:
if re.match(r'^(\(\d{3}\) \d{3}-\d{4}|\d{3}-\d{3}-\d{4})
```, line.st... | 2 | 0 | ['Python', 'Python3', 'Bash'] | 1 |
valid-phone-numbers | [Bash] one-liner | bash-one-liner-by-kevin-shu-i35l | \ngrep -P \'(^\\(\\d{3}\\) \\d{3}\\-\\d{4}$)|(^\\d{3}\\-\\d{3}\\-\\d{4}$)\' file.txt\n | kevin-shu | NORMAL | 2022-10-16T08:14:49.137248+00:00 | 2022-10-16T08:14:49.137279+00:00 | 2,667 | false | ```\ngrep -P \'(^\\(\\d{3}\\) \\d{3}\\-\\d{4}$)|(^\\d{3}\\-\\d{3}\\-\\d{4}$)\' file.txt\n``` | 2 | 0 | [] | 1 |
valid-phone-numbers | One Line GREP Command 😉 | one-line-grep-command-by-ashwani_kumar-njz5 | grep -P \'^(\\d{3}-|\\(\\d{3}\\) )\\d{3}-\\d{4}$\' file.txt | Ashwani_Kumar | NORMAL | 2022-08-09T06:03:12.704840+00:00 | 2022-08-09T06:03:53.631343+00:00 | 1,558 | false | `grep -P \'^(\\d{3}-|\\(\\d{3}\\) )\\d{3}-\\d{4}$\' file.txt` | 2 | 0 | [] | 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.