From 32499b85c880948d77a233fb966e082c4c97c219 Mon Sep 17 00:00:00 2001 From: Tahir <83111631+Tahirc1@users.noreply.github.com> Date: Thu, 26 Oct 2023 11:04:07 +0530 Subject: [PATCH 1/7] Update UNSOLVED.md --- UNSOLVED.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/UNSOLVED.md b/UNSOLVED.md index 4081b4f..1689c4a 100644 --- a/UNSOLVED.md +++ b/UNSOLVED.md @@ -1,9 +1,5 @@ ## Unsolved Problems **LeetCode problems to be solved** -
- 1-10 - 10. Regular Expression Matching -
11-20 16. 3Sum Closest
From 08a732286f8ee37407e95c4875e2ac92a348d1ef Mon Sep 17 00:00:00 2001 From: Tahir <83111631+Tahirc1@users.noreply.github.com> Date: Sun, 20 Oct 2024 14:56:47 +0530 Subject: [PATCH 2/7] Update 15. 3Sum.js --- 11-20/15. 3Sum.js | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/11-20/15. 3Sum.js b/11-20/15. 3Sum.js index 7361881..deb49f2 100644 --- a/11-20/15. 3Sum.js +++ b/11-20/15. 3Sum.js @@ -1,22 +1,42 @@ var threeSum = function(nums) { + // Create array a to store output array let a = [] + // Create variable j and k for indexing nums[j] and nums[k] + // Create val1 and val2 for storing value of nums[j] and nums[k] respectively let [j,k,val1,val2] = [0,0,0,0] + // sort the input array ascendingly nums.sort(function(a,b){return a-b}) - for(let i = 0 ;i < nums.length-2;i++){ + // run loop over nums from (0,nums.length-2) + // we need i , j , k so last usable index of will be nums.length-2 + // storing length helps a little bit for optimization + len = nums.length + for(let i = 0 ;i < len-2;i++){ + // nums[i] must be negative or 0 to get nums[i] + nums[j] + nums[k] == 0 if(nums[i] > 0 ){continue} + // we will cover all possible triplet for nums[i] so we dont need to rerun for same value if(nums[i] == nums[i-1]){continue} + // j will be the start pointer and k will be end pointer for our window j = 1 + i - k = nums.length - 1 + k = len - 1 + // we will run this until the whole window is convered while(j < k ){ if(nums[i] + nums[j] + nums[k] == 0){ + // if this condition is met we add it to answer array a.push([nums[i] , nums[j] , nums[k]]) + // we store value of nums[j] in val1 val1=nums[j]; + // we skip all index with same value as nums[j] this will help up shorten the window while(j 0 we try to get a smaller value for nums[k] (array is sorted ascending) else if(nums[i]+nums[j]+nums[k]>0) k--; + // thus when this loop is done we will have all possible j and k for given nums[i] } } + // return the output array return a -}; \ No newline at end of file +}; From d81656ad21a1ed94893f53d4f17995d24941bb15 Mon Sep 17 00:00:00 2001 From: Tahir <83111631+Tahirc1@users.noreply.github.com> Date: Sun, 20 Oct 2024 15:37:21 +0530 Subject: [PATCH 3/7] Update 17. Letter Combinations of a Phone Number.js --- ...17. Letter Combinations of a Phone Number.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/11-20/17. Letter Combinations of a Phone Number.js b/11-20/17. Letter Combinations of a Phone Number.js index 1beb0d7..072d537 100644 --- a/11-20/17. Letter Combinations of a Phone Number.js +++ b/11-20/17. Letter Combinations of a Phone Number.js @@ -1,16 +1,31 @@ var letterCombinations = function(digits) { + // if digits is empty return [] if(digits == ""){return []} + // we create all possible strings a digit might correspond to + // digit contains number 2-9 so we keep index 0 and 1 as 0 let k = [0,0,"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"] + // we convert first digit to number let m = Number(digits[0]) + // we split the corresponding string to alphabets + // and it will store all output let a = k[m].split("") let [p,n] = [[],[]] + // we loop over all digits from index 1 to last index for(let i = 1 ; i < digits.length ;i++){ + // p will store all possible alphabets for current digit p = k[digits[i]].split("") + // n will store all the possible combinations made for that digit n = [] + // all possible combinations for given digit are are p.length*a.length for(let j = 0 ; j < p.length*a.length ;j++){ + // Math.floor(j/p.length) this let us stay in range of index 0-a.length-1 + // j%p.length this let us stay in range of index 0-p.length-1 + // thus going over all possible combination for all given a and p indices n.push(a[Math.floor(j/p.length)]+p[j%p.length]) } + // we update the array a will all current possiblities a = n } + // we return all possible combinations return a -}; \ No newline at end of file +}; From 919a13da295370ca0d63a0963cdf308d1a85a8e1 Mon Sep 17 00:00:00 2001 From: Tahir <83111631+Tahirc1@users.noreply.github.com> Date: Mon, 21 Oct 2024 13:41:10 +0530 Subject: [PATCH 4/7] Update 26. Remove Duplicates from Sorted Array.js Adding explanation to problem 26 --- 21-30/26. Remove Duplicates from Sorted Array.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/21-30/26. Remove Duplicates from Sorted Array.js b/21-30/26. Remove Duplicates from Sorted Array.js index 89340c3..b8b315e 100644 --- a/21-30/26. Remove Duplicates from Sorted Array.js +++ b/21-30/26. Remove Duplicates from Sorted Array.js @@ -1,8 +1,12 @@ var removeDuplicates = function(nums) { + // Create a set from nums array let s = new Set(nums) + // convert that set to Array let num = Array.from(s.values()) + // replace nums[i] with num[i] we need to update nums for(let i =0; i < num.length ; i++){ nums[i] = num[i] } + // return num.length return num.length -}; \ No newline at end of file +}; From be7108e9d5597894cb93ab5745811c926e608c9e Mon Sep 17 00:00:00 2001 From: Tahir <83111631+Tahirc1@users.noreply.github.com> Date: Mon, 28 Oct 2024 13:36:24 +0530 Subject: [PATCH 5/7] Update 27. Remove Element.js add explanation to problem 27 --- 21-30/27. Remove Element.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/21-30/27. Remove Element.js b/21-30/27. Remove Element.js index 9360878..9f59241 100644 --- a/21-30/27. Remove Element.js +++ b/21-30/27. Remove Element.js @@ -1,10 +1,15 @@ var removeElement = function(nums, val) { + // we have to return number of elements not equal to val in nums + // while removing all occurenece of val in place let k = 0 + // run a for loop over nums for(let i = 0; i < nums.length ;i++){ + // when nums[i] is not equal to val assign nums[k] with nums[i] + // k is position(and counter) for value which are not equal to k if(!(nums[i] == val)){ nums[k] = nums[i] k++ } } return k -}; \ No newline at end of file +}; From 4cc5d18f1cfad14e18d7953dfe79106dcc51899f Mon Sep 17 00:00:00 2001 From: Tahir <83111631+Tahirc1@users.noreply.github.com> Date: Mon, 28 Oct 2024 13:40:09 +0530 Subject: [PATCH 6/7] Update 28. Find the Index of the First Occurrence in a String.js --- .../28. Find the Index of the First Occurrence in a String.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/21-30/28. Find the Index of the First Occurrence in a String.js b/21-30/28. Find the Index of the First Occurrence in a String.js index 6f4b41f..f5a0ba9 100644 --- a/21-30/28. Find the Index of the First Occurrence in a String.js +++ b/21-30/28. Find the Index of the First Occurrence in a String.js @@ -1,3 +1,4 @@ var strStr = function(haystack, needle) { + // we will use .indexOf() method of array return haystack.indexOf(needle) -}; \ No newline at end of file +}; From f7781471c7139b9cfaaa9a47ee15d5cf706690b2 Mon Sep 17 00:00:00 2001 From: Tahir <83111631+Tahirc1@users.noreply.github.com> Date: Mon, 28 Oct 2024 13:49:43 +0530 Subject: [PATCH 7/7] Update 36. Valid Sudoku.js adding explanation to problem 36 --- 31-40/36. Valid Sudoku.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/31-40/36. Valid Sudoku.js b/31-40/36. Valid Sudoku.js index ffd9ba4..3f29c7c 100644 --- a/31-40/36. Valid Sudoku.js +++ b/31-40/36. Valid Sudoku.js @@ -1,23 +1,29 @@ var isValidSudoku = function(board) { + // create variable to store row , col and box values let row = [] let col = [] let box = [] + // loop i over 0-8 for (let i = 0; i <= 8; i++){ + // loop j over 0-8 for (let j = 0; j <= 8; j++){ + // this will help us get position of each cell from board let IndI = 3*Math.floor(i/3) + Math.floor(j/3) let IndJ = 3*(i % 3) + (j % 3) + // check if value is already in row , col or box and return false if (row.indexOf(board[i][j]) != -1 ){return false} if (col.indexOf(board[j][i]) != -1 ){return false} if (box.indexOf(board[IndI][IndJ]) != -1){return false} - + // if not then add value to row , col and box if(board[i][j] != '.'){row.push(board[i][j])} if(board[j][i] != '.'){col.push(board[j][i])} if(board[IndI][IndJ] != '.'){box.push(board[IndI][IndJ])} } + // reset row , col and box array row = [] col = [] box = [] } return true -} \ No newline at end of file +}