From 174f6a7f70402f0436c9c77df4ca32323e0a0bf3 Mon Sep 17 00:00:00 2001 From: tahir Date: Fri, 13 Oct 2023 11:54:04 +0530 Subject: [PATCH 01/22] 66th problem --- 61-70/62. Unique Paths.js | 14 ++++++++++++++ 61-70/65. Valid Number.js | 3 +++ 61-70/66. Plus One.js | 3 +++ 3 files changed, 20 insertions(+) create mode 100644 61-70/62. Unique Paths.js create mode 100644 61-70/65. Valid Number.js create mode 100644 61-70/66. Plus One.js diff --git a/61-70/62. Unique Paths.js b/61-70/62. Unique Paths.js new file mode 100644 index 0000000..5f469cd --- /dev/null +++ b/61-70/62. Unique Paths.js @@ -0,0 +1,14 @@ +var uniquePaths = function(m, n) { + let a = [1,2,6] + let fac = (k)=>{ + if(k < a.length){return } + let p= a.length + while(k > a.length){ + a.push( (p+1) * a[p-1] ) + p++ + } + } + fac(m+n-2) + let res = ((a[m+n-3]/(a[m-2]))/a[n-2]) + return (m===1||n===1) ? 1 : res +}; \ No newline at end of file diff --git a/61-70/65. Valid Number.js b/61-70/65. Valid Number.js new file mode 100644 index 0000000..8085abf --- /dev/null +++ b/61-70/65. Valid Number.js @@ -0,0 +1,3 @@ +var isNumber = function(s) { + return !isNaN(s) && !s.includes("infinity") && !s.includes("Infinity"); +}; \ No newline at end of file diff --git a/61-70/66. Plus One.js b/61-70/66. Plus One.js new file mode 100644 index 0000000..5faa455 --- /dev/null +++ b/61-70/66. Plus One.js @@ -0,0 +1,3 @@ +var plusOne = function(digits) { + return (BigInt(digits.join("")) + BigInt(1)).toString().split("") +}; \ No newline at end of file From b66aeb98b6b31c84c1f8ce2a5d971b6b046003e0 Mon Sep 17 00:00:00 2001 From: tahir Date: Fri, 13 Oct 2023 12:06:34 +0530 Subject: [PATCH 02/22] 73/81 problem --- 71-80/73. Set Matrix Zeroes.js | 22 +++++++++++++++++++ .../81. Search in Rotated Sorted Array II.js | 4 ++++ 2 files changed, 26 insertions(+) create mode 100644 71-80/73. Set Matrix Zeroes.js create mode 100644 81-90/81. Search in Rotated Sorted Array II.js diff --git a/71-80/73. Set Matrix Zeroes.js b/71-80/73. Set Matrix Zeroes.js new file mode 100644 index 0000000..2e7b5fd --- /dev/null +++ b/71-80/73. Set Matrix Zeroes.js @@ -0,0 +1,22 @@ +var setZeroes = function(matrix) { + let [r,c] = [new Set(),new Set()] + let [col,row] = [matrix[0].length,matrix.length] + for(let i = 0 ; i < row; i++){ + for(let j = 0 ; j < col; j++){ + if(matrix[i][j] == 0){ + r.add(i);c.add(j) + } + } + } + let rz = (n) =>{ + for(let i = 0; i < col; i++){matrix[n][i] = 0} + } + let cz = (n) =>{ + for(let i = 0; i < row; i++){matrix[i][n] = 0} + } + r = Array.from(r.values()) + c = Array.from(c.values()) + for(let i = 0; i < r.length; i++){rz(r[i])} + for(let i = 0; i < c.length; i++){cz(c[i])} + return matrix +}; \ No newline at end of file diff --git a/81-90/81. Search in Rotated Sorted Array II.js b/81-90/81. Search in Rotated Sorted Array II.js new file mode 100644 index 0000000..a6bc083 --- /dev/null +++ b/81-90/81. Search in Rotated Sorted Array II.js @@ -0,0 +1,4 @@ +var search = function(nums, target) { + // Set.has() returns true if value is in the set + return new Set(nums).has(target) +}; \ No newline at end of file From 6da4bdd17b73e68c9397c6446780fe812a6d13b8 Mon Sep 17 00:00:00 2001 From: tahir Date: Fri, 13 Oct 2023 12:11:31 +0530 Subject: [PATCH 03/22] 92nd problem --- 91-100/92. Reverse Linked List II.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 91-100/92. Reverse Linked List II.js diff --git a/91-100/92. Reverse Linked List II.js b/91-100/92. Reverse Linked List II.js new file mode 100644 index 0000000..663143f --- /dev/null +++ b/91-100/92. Reverse Linked List II.js @@ -0,0 +1,24 @@ +var reverseBetween = function(head, left, right) { + let [a,b] = [[],[]] + let k = new ListNode(0) + let ans = k + while(head != null){ + a.push(head.val) + head = head.next + } + for(let i = 0 ; i < a.length ;i++){ + if(i >= left-1 && i <= right-1){ + b.push(a[i]) + } + } + b = b.reverse() + let o = 0 + for(let i = 0 ; i < a.length ;i++){ + if(i >= left-1 && i <= right-1){ + k = k.next = new ListNode(b[o++]) + }else{ + k = k.next = new ListNode(a[i]) + } + } + return ans.next +}; \ No newline at end of file From 64149c46fd04469b3f2c643a65d03a367335ec04 Mon Sep 17 00:00:00 2001 From: Tahir <83111631+Tahirc1@users.noreply.github.com> Date: Fri, 13 Oct 2023 12:19:12 +0530 Subject: [PATCH 04/22] adding comments 19. Remove Nth Node From End of List.js --- 11-20/19. Remove Nth Node From End of List.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/11-20/19. Remove Nth Node From End of List.js b/11-20/19. Remove Nth Node From End of List.js index 9335ac7..d3032c9 100644 --- a/11-20/19. Remove Nth Node From End of List.js +++ b/11-20/19. Remove Nth Node From End of List.js @@ -1,17 +1,20 @@ var removeNthFromEnd = function(head, n) { - let count = 0 - let m = head + let count = 0 // counter + let m = head // store head node + // count no. of nodes while(head != null){ - head = head.next - count++ + head = head.next // travel to next node + count++ // increment counter } + // create new ListNode let k = new ListNode(0) - let ans = k + let ans = k // store head of k list for(let i = 0 ; i < count ; i++){ + // if i is not nth node from end we add that node to k list if(i != count - n){ k = k.next = new ListNode(m.val) } - m = m.next + m = m.next // travel to next node } return ans.next -}; \ No newline at end of file +}; From 1a7663d0ffa6495d93e23c2aef73f86ceb03c766 Mon Sep 17 00:00:00 2001 From: Tahir <83111631+Tahirc1@users.noreply.github.com> Date: Fri, 13 Oct 2023 12:24:32 +0530 Subject: [PATCH 05/22] adding comments 20. Valid Parentheses.js --- 11-20/20. Valid Parentheses.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/11-20/20. Valid Parentheses.js b/11-20/20. Valid Parentheses.js index a3361f1..2fa90f3 100644 --- a/11-20/20. Valid Parentheses.js +++ b/11-20/20. Valid Parentheses.js @@ -1,10 +1,16 @@ var isValid = function(s) { - let l = s.length+1; + let l = s.length+1; // store length+1 + // while l is not equal to s.length while(l !== s.length) { - l = s.length; + l = s.length; // store s.length in l + // when we replace () or {} or [] with '' + // the length of string s is reduced s = s.replaceAll('()',''); s = s.replaceAll('{}',''); s = s.replaceAll('[]',''); + // if nothing is replaced l equals s.length + // thus ending the while loop } + // if s.length becomes 0 thus string had valid parentheses return s.length === 0; -} \ No newline at end of file +} From 51dd34a6c7a63bf4ac1dbedda5459ad9334effb1 Mon Sep 17 00:00:00 2001 From: Tahir <83111631+Tahirc1@users.noreply.github.com> Date: Fri, 13 Oct 2023 17:13:23 +0530 Subject: [PATCH 06/22] Create UNSOLVED.md --- UNSOLVED.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 UNSOLVED.md diff --git a/UNSOLVED.md b/UNSOLVED.md new file mode 100644 index 0000000..4081b4f --- /dev/null +++ b/UNSOLVED.md @@ -0,0 +1,67 @@ +## Unsolved Problems +**LeetCode problems to be solved** +
+ 1-10 + 10. Regular Expression Matching +
+
+ 11-20 + 16. 3Sum Closest
+ 18. 4Sum +
+
+ 21-30 + 22. Generate Parentheses
+ 30. Substring with Concatenation of All Words +
+
+ 31-40 + 31. Next Permutation
+ 32. Longest Valid Parentheses
+ 37. Sudoku Solver
+ 39. Combination Sum
+ 40. Combination Sum II +
+
+ 41-50 + 42. Trapping Rain Water
+ 44. Wildcard Matching
+ 46. Permutations
+ 47. Permutations II +
+
+ 51-60 + 51. N-Queens
+ 52. N-Queens II
+ 56. Merge Intervals
+ 59. Spiral Matrix II
+ 60. Permutation Sequence +
+
+ 61-70 + 63. Unique Paths II
+ 64. Minimum Path Sum +
+
+ 71-80 + 71. Simplify Path
+ 72. Edit Distance
+ 76. Minimum Window Substring +
+
+ 81-90 + 84. Largest Rectangle in Histogram
+ 85. Maximal Rectangle
+ 87. Scramble String
+ 89. Gray Code
+ 90. Subsets II +
+
+ 91-100 + 91. Decode Ways
+ 93. Restore IP Addresses
+ 95. Unique Binary Search Trees II
+ 96. Unique Binary Search Trees
+ 97. Interleaving String
+ 99. Recover Binary Search Tree +
From 638bbcf91b6708220bcf56d199d4a5ca1f64e9a5 Mon Sep 17 00:00:00 2001 From: Tahir <83111631+Tahirc1@users.noreply.github.com> Date: Fri, 13 Oct 2023 17:18:01 +0530 Subject: [PATCH 07/22] Update README.md --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1589c9e..bf9c66a 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@ # LeetcodeJs Library for LeetCode Questions solved in JS with comments -Anyone can add Leetcode solutions in JS or add comments to existing solutions +Anyone can add Leetcode solutions in JS or add comments to existing solutions\ +UNSOLVED.md contains the unsolved problems Follow naming convention \ -'Question Number'+'. '+'Question Name'\ -\ -Add ur Name and Socials on top 2 lines as a comment +- 'Question Number'+'. '+'Question Name'\ + +Add your Name and Socials on top 2 lines as a comment From 56026180c0a4bbb71feb6c446c2f2eed6e548b08 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 14 Oct 2023 09:42:16 +0100 Subject: [PATCH 08/22] add doc to 21 --- 21-30/21. Merge Two Sorted Lists.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/21-30/21. Merge Two Sorted Lists.js b/21-30/21. Merge Two Sorted Lists.js index 66aef6b..e46c98b 100644 --- a/21-30/21. Merge Two Sorted Lists.js +++ b/21-30/21. Merge Two Sorted Lists.js @@ -1,21 +1,32 @@ var mergeTwoLists = function(list1, list2) { + // add the list node to a variable let l = new ListNode(0); let head = l + // create a while loop and check if the argument (list1 and list2) is not empty while(list1 != null && list2 != null){ + // if the value of list1 is less than list2 if(list1.val < list2.val){ + //add the value to the node list of 1 l.next = list1 list1 = list1.next }else{ + // if not add the value to the node list of 2 l.next = list2 list2 = list2.next } + + //move on to the next node l = l.next } + + //add the remaining node values to the merged list if (list1 == null) { l.next = list2; } else { l.next = list1; } + + //return the combined sorted list return head.next }; \ No newline at end of file From 70686f1a5475a6aff6400e3c7a3d2d2e366a66b6 Mon Sep 17 00:00:00 2001 From: tahir Date: Tue, 17 Oct 2023 12:29:26 +0530 Subject: [PATCH 09/22] 68th problem --- 61-70/67. Add Binary.js | 4 ++++ 61-70/68. Text Justification.js | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 61-70/67. Add Binary.js create mode 100644 61-70/68. Text Justification.js diff --git a/61-70/67. Add Binary.js b/61-70/67. Add Binary.js new file mode 100644 index 0000000..5e77055 --- /dev/null +++ b/61-70/67. Add Binary.js @@ -0,0 +1,4 @@ +var addBinary = function(a, b) { + let sum = BigInt(`0b${a}`) + BigInt(`0b${b}`); + return sum.toString(2); +}; \ No newline at end of file diff --git a/61-70/68. Text Justification.js b/61-70/68. Text Justification.js new file mode 100644 index 0000000..99a28ab --- /dev/null +++ b/61-70/68. Text Justification.js @@ -0,0 +1,25 @@ +var fullJustify = function(words, maxWidth) { + let [line,spaces,addSpace,minSpaces] = [0,0,0,""] + for (let res = [[]], i = 0, letters = 0; i <= words.length; letters += words[i++].length) { + let row = res[res.length - 1]; + let len = row.length + if (i == words.length || len && letters + len + words[i].length > maxWidth) { + if (len == 1 || i == words.length) { + res[res.length - 1] = row.join(' ') + ' '.repeat(maxWidth - letters - len + 1); + if (i === words.length) return res; + } else { + line = row[0]; + spaces = maxWidth - letters; + minSpaces = ' '.repeat(Math.floor(spaces / (len- 1))); + addSpace = spaces % (len - 1); + for (let w = 1; w < len; w++) { + line += minSpaces + (w <= addSpace ? ' ' : '') + row[w]; + } + res[res.length - 1] = line; + } + res.push([]); + letters = 0; + } + res[res.length - 1].push(words[i]); + } +}; \ No newline at end of file From ef5e6eea463b926a9ea9ad8f9edce2a1162c3dd4 Mon Sep 17 00:00:00 2001 From: tahir Date: Wed, 18 Oct 2023 12:33:33 +0530 Subject: [PATCH 10/22] 70th problem --- 61-70/69. Sqrt(x).js | 7 +++++++ 61-70/70. Climbing Stairs.js | 6 ++++++ 2 files changed, 13 insertions(+) create mode 100644 61-70/69. Sqrt(x).js create mode 100644 61-70/70. Climbing Stairs.js diff --git a/61-70/69. Sqrt(x).js b/61-70/69. Sqrt(x).js new file mode 100644 index 0000000..0484132 --- /dev/null +++ b/61-70/69. Sqrt(x).js @@ -0,0 +1,7 @@ +var mySqrt = function(x) { + let r = x + while(r*r > x ){ + r = Math.floor((r + x/r) / 2) + } + return r; +}; \ No newline at end of file diff --git a/61-70/70. Climbing Stairs.js b/61-70/70. Climbing Stairs.js new file mode 100644 index 0000000..695a2ce --- /dev/null +++ b/61-70/70. Climbing Stairs.js @@ -0,0 +1,6 @@ +var climbStairs = function(n) { + if(n < 4){return n} + let arr = [1,2,3] + for(let i=4; i<= n;i++) {arr[i-1] = (arr[i-2] + arr[i-3]);} + return arr[arr.length -1]; +}; \ No newline at end of file From e066e5eae49cf87acba81df4fe82266f3ee38e1f Mon Sep 17 00:00:00 2001 From: Tahir <83111631+Tahirc1@users.noreply.github.com> Date: Fri, 20 Oct 2023 11:21:50 +0530 Subject: [PATCH 11/22] update code & comment 23. Merge k Sorted Lists.js --- 21-30/23. Merge k Sorted Lists.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/21-30/23. Merge k Sorted Lists.js b/21-30/23. Merge k Sorted Lists.js index cd0a2b4..cec4e85 100644 --- a/21-30/23. Merge k Sorted Lists.js +++ b/21-30/23. Merge k Sorted Lists.js @@ -1,19 +1,25 @@ var mergeKLists = function(lists) { - let a = [] + let a = [] // create an array + // loop over all the lists for(let i = 0 ; i < lists.length ; i++){ - let k = lists[i] + let k = lists[i] // store list in k + // loop till end of list in k while(k != null){ + // push listnode value in a a.push(k.val) + // go to next node k = k.next } } + // sort the array a.sort(function(a,b){return a-b}) - let m = new ListNode(0) - let ans = m + let m = new ListNode(0) // create listnode + let ans = m // store head of listnode in ans + // loop the array for(let j = 0 ; j< a.length ;j++){ - let n = new ListNode(a[j]) - m.next = n - m = m.next + // assign array j value to next listnode and go to next node + m = m.next = new ListNode(a[j]) } + // return ans List return ans.next -}; \ No newline at end of file +}; From 9fbab57305c53362422d2f6fcda15be535c6ab4e Mon Sep 17 00:00:00 2001 From: tahir Date: Fri, 20 Oct 2023 11:38:27 +0530 Subject: [PATCH 12/22] 73rd problem --- 71-80/74. Search a 2D Matrix.js | 13 +++++++++++++ 71-80/75. Sort Colors.js | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 71-80/74. Search a 2D Matrix.js create mode 100644 71-80/75. Sort Colors.js diff --git a/71-80/74. Search a 2D Matrix.js b/71-80/74. Search a 2D Matrix.js new file mode 100644 index 0000000..f408c63 --- /dev/null +++ b/71-80/74. Search a 2D Matrix.js @@ -0,0 +1,13 @@ +var searchMatrix = function(matrix, target) { + for(let i = 0 ; i < matrix.length ; i++){ + if(target > matrix[i][matrix[0].length-1] ){ + continue + } + for(let j = 0 ; j < matrix[0].length ; j++){ + if(matrix[i][j] == target){ + return true + } + } + } + return false +}; \ No newline at end of file diff --git a/71-80/75. Sort Colors.js b/71-80/75. Sort Colors.js new file mode 100644 index 0000000..c2c57e9 --- /dev/null +++ b/71-80/75. Sort Colors.js @@ -0,0 +1,17 @@ +var sortColors = function(nums) { + let obj = {} + let l = nums.length + if(l == 1){return nums} + for(let i = 0 ; i < l ;i++){ + obj[nums[i]]= (obj[nums[i]])?obj[nums[i]]+1:1 + } + let j = 0 + for(let x in obj){ + while(obj[x] > 0){ + if(nums[j] < x){} + nums[j] = +x + obj[x]-- + j++ + } + } +}; \ No newline at end of file From ef76ee6f9c953f6742f7b55e2775686adedb6bfb Mon Sep 17 00:00:00 2001 From: tahir Date: Sat, 21 Oct 2023 20:09:03 +0530 Subject: [PATCH 13/22] 80th problem --- 71-80/77. Combinations.js | 14 ++++++++++ 71-80/78. Subsets.js | 13 ++++++++++ 71-80/79. Word Search.js | 26 +++++++++++++++++++ ... Remove Duplicates from Sorted Array II.js | 18 +++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 71-80/77. Combinations.js create mode 100644 71-80/78. Subsets.js create mode 100644 71-80/79. Word Search.js create mode 100644 71-80/80. Remove Duplicates from Sorted Array II.js diff --git a/71-80/77. Combinations.js b/71-80/77. Combinations.js new file mode 100644 index 0000000..47fa21f --- /dev/null +++ b/71-80/77. Combinations.js @@ -0,0 +1,14 @@ +var combine = function(n, k) { + let result = [] + let traverse = (arr, depth) =>{ + if (arr.length === k) { + result.push(arr) + return + } + if (depth > n) {return} + traverse(arr, depth + 1) + traverse(arr.concat(depth), depth + 1) + } + traverse([], 1) + return result; +}; \ No newline at end of file diff --git a/71-80/78. Subsets.js b/71-80/78. Subsets.js new file mode 100644 index 0000000..882eeee --- /dev/null +++ b/71-80/78. Subsets.js @@ -0,0 +1,13 @@ +var subsets = function(nums) { + let ans = [[]] + let k = [] + for(let i = 0 ; i < nums.length ; i++){ + let n = ans.length + for(let j = 0 ; j < n ;j++){ + k = Array.from(ans[j]) + k.push(nums[i]) + ans.push(k) + } + } + return ans +}; \ No newline at end of file diff --git a/71-80/79. Word Search.js b/71-80/79. Word Search.js new file mode 100644 index 0000000..ceec009 --- /dev/null +++ b/71-80/79. Word Search.js @@ -0,0 +1,26 @@ +var exist = function(board, word) { + let found = false; + const backtrack = (board, rIdx, cIdx, word, letterIdx) => { + if(rIdx >= 0 && rIdx < board.length && cIdx >=0 && cIdx < board[0].length && board[rIdx][cIdx] === word[letterIdx]) { + if(letterIdx === word.length-1) found ||= true; + const curLetter = word[letterIdx]; + board[rIdx][cIdx] = ''; + backtrack(board, rIdx+1, cIdx, word, letterIdx+1); + backtrack(board, rIdx-1, cIdx, word, letterIdx+1); + backtrack(board, rIdx, cIdx+1, word, letterIdx+1); + backtrack(board, rIdx, cIdx-1, word, letterIdx+1); + board[rIdx][cIdx] = curLetter; + } + }; + + let letterIdx = 0; + board.forEach((row, rIdx) => { + row.forEach((cell, cIdx) => { + if(cell === word[letterIdx]) { + backtrack(board, rIdx, cIdx, word, letterIdx); + if(found) return found; + } + }) + }); + return found; +} \ No newline at end of file diff --git a/71-80/80. Remove Duplicates from Sorted Array II.js b/71-80/80. Remove Duplicates from Sorted Array II.js new file mode 100644 index 0000000..3923076 --- /dev/null +++ b/71-80/80. Remove Duplicates from Sorted Array II.js @@ -0,0 +1,18 @@ +var removeDuplicates = function(nums) { + let j = 0 + let [m,c,k] = [0,0,0] + for(let i = 0 ; i Date: Wed, 25 Oct 2023 14:33:22 +0530 Subject: [PATCH 14/22] 88th problem --- .... Remove Duplicates from Sorted List II.js | 18 +++++++++++++++++ .../83. Remove Duplicates from Sorted List.js | 13 ++++++++++++ 81-90/86. Partition List.js | 20 +++++++++++++++++++ 81-90/88. Merge Sorted Array.js | 9 +++++++++ 4 files changed, 60 insertions(+) create mode 100644 81-90/82. Remove Duplicates from Sorted List II.js create mode 100644 81-90/83. Remove Duplicates from Sorted List.js create mode 100644 81-90/86. Partition List.js create mode 100644 81-90/88. Merge Sorted Array.js diff --git a/81-90/82. Remove Duplicates from Sorted List II.js b/81-90/82. Remove Duplicates from Sorted List II.js new file mode 100644 index 0000000..da6ff65 --- /dev/null +++ b/81-90/82. Remove Duplicates from Sorted List II.js @@ -0,0 +1,18 @@ +var deleteDuplicates = function(head) { + let obj = {} + while(head != null){ + obj[head.val] = (obj[head.val])?obj[head.val]+1:1 + head = head.next + } + let a = [] + for(let c in obj){ + if(obj[c] == 1){a.push(c)} + } + a.sort((a,b)=>{return a-b}) + let n = new ListNode() + let ans = n + for(let i = 0 ; i < a.length ; i++){ + n = n.next = new ListNode(a[i]) + } + return ans.next +}; \ No newline at end of file diff --git a/81-90/83. Remove Duplicates from Sorted List.js b/81-90/83. Remove Duplicates from Sorted List.js new file mode 100644 index 0000000..ef619a4 --- /dev/null +++ b/81-90/83. Remove Duplicates from Sorted List.js @@ -0,0 +1,13 @@ +var deleteDuplicates = function(head) { + let a = new Set() + let b = new ListNode(0) + let c = b + while(head != null ){ + a.add(head.val) + head = head.next + } + for(let x of a){ + b = b.next = new ListNode(x) + } + return c.next +}; \ No newline at end of file diff --git a/81-90/86. Partition List.js b/81-90/86. Partition List.js new file mode 100644 index 0000000..52526a7 --- /dev/null +++ b/81-90/86. Partition List.js @@ -0,0 +1,20 @@ +var partition = function(head, x) { + if(head === null) return head; + const leftDummy = new ListNode(0); + const rightDummy = new ListNode(0); + let left = leftDummy; + let right = rightDummy; + while(head){ + if(head.val < x) { + left.next = head; + left = head; + } else { + right.next = head; + right = head; + } + head = head.next; + } + right.next = null; + left.next = rightDummy.next; + return leftDummy.next; +}; \ No newline at end of file diff --git a/81-90/88. Merge Sorted Array.js b/81-90/88. Merge Sorted Array.js new file mode 100644 index 0000000..5c71551 --- /dev/null +++ b/81-90/88. Merge Sorted Array.js @@ -0,0 +1,9 @@ +var merge = function(nums1, m, nums2, n){ + nums1.length = m + nums2.length = n + let ans = nums1.concat(nums2) + ans.sort((a,b)=>a-b) + for(let i = 0 ; i < ans.length ; i++){ + nums1[i] = ans[i] + } +}; \ No newline at end of file From a4593b148b350400f76b80e7e54127ca0e608c27 Mon Sep 17 00:00:00 2001 From: 96vksinghcort <147235888+96vksinghcort@users.noreply.github.com> Date: Wed, 25 Oct 2023 16:12:31 +0530 Subject: [PATCH 15/22] added 10. Regular Expression Matching.js --- 1-10/10. Regular Expression Matching.js | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1-10/10. Regular Expression Matching.js diff --git a/1-10/10. Regular Expression Matching.js b/1-10/10. Regular Expression Matching.js new file mode 100644 index 0000000..a1297a2 --- /dev/null +++ b/1-10/10. Regular Expression Matching.js @@ -0,0 +1,39 @@ +function isMatch(str, pat) { + return recursiveIsMatch(0, 0, str, pat); +} +function recursiveIsMatch(i, j, str, pat) { + const inputStringLength = str.length; + const patternLength = pat.length; + + // Reached the end of the pattern + if (j == patternLength) { + // Return whether or not we've also reached the end of the string (entire string has passed) + return i == inputStringLength; + } + + // If the current pattern character is followed by a * (is a wildcard) + if (j + 1 < patternLength && pat.charAt(j + 1) == '*') { + // Assume 0 matches of the current pattern character, move on to the next point in the pattern (after the asterisk) + if (recursiveIsMatch(i, j + 2, str, pat)) return true; + + // Loop through the remaining characters, so long as they match by character (or .) + while ( + i < inputStringLength && + (pat.charAt(j) == '.' || str.charAt(i) == pat.charAt(j)) + ) { + // Check the rest of the string (1 character forward), against the next point in the pattern (after the asterisk) + if (recursiveIsMatch(++i, j + 2, str, pat)) return true; + } + } + // If the current pattern character is not a wildcard, and matches the current string character + else if ( + i < inputStringLength && + (pat.charAt(j) == '.' || str.charAt(i) == pat.charAt(j)) + ) { + // Move onto the next character, and the next character of the pattern + return recursiveIsMatch(i + 1, j + 1, str, pat); + } + + // String does not match current point in pattern + return false; +} 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 16/22] 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 17/22] 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 18/22] 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 19/22] 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 20/22] 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 21/22] 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 22/22] 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 +}