├── Arrays ├── 11_container_with_most_water.js ├── 121_best_time_to_buy_and_sell_stocks.js ├── 152_maximum_product_subarray.js ├── 153_find_minimum_in_rotated_sorted_array.js ├── 15_3Sum.js ├── 1_two_sum.js ├── 217_contains_duplicate.js ├── 238_product_of_array_except_self.js ├── 33_search_in_rotated_sorted_array.js └── 53_maximum_subarray.js ├── Binary ├── 190_reverse_bits.js ├── 191_number_of_1_bits.js ├── 268_missing_number.js ├── 338_counting_bits.js └── 371_sum_of_two_integers.js ├── Heap ├── 23_merge_k_sorted_lists.js ├── 295_find_median_from_data_streams.js └── 347_top_k_frequent_elements.js ├── Matrix ├── 48_rotate_image.js ├── 54_spiral_matrix.js ├── 73_set_matrix_zeroes.js └── 79_word_search.js ├── Readme.md └── Strings ├── 125_valid_palindrome.js ├── 20_valid_parentheses.js ├── 242_valid_anagram.js ├── 3_longest_substring_with_repeating_character.js ├── 424_longest_repeating_character_replacement.js ├── 49_group_anagrams.js ├── 5_longest_palandromic_substring.js └── 647_palindromic_substrings.js /Arrays/11_container_with_most_water.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} height 3 | * @return {number} 4 | */ 5 | var maxArea = function(height) { 6 | let max = 0; 7 | let left = 0; 8 | let right = height.length - 1; 9 | while(left < right){ 10 | let lh = height[left]; 11 | let rh = height[right]; 12 | maxWater = ( right - left) * Math.min(lh, rh); 13 | if(lh < rh) left++; 14 | else right--; 15 | 16 | max = Math.max(maxWater, max); 17 | } 18 | return max; 19 | 20 | }; -------------------------------------------------------------------------------- /Arrays/121_best_time_to_buy_and_sell_stocks.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} prices 3 | * @return {number} 4 | */ 5 | var maxProfit = function(prices) { 6 | // required to find maximum profit and minimum buy price 7 | let maxProfit = 0; 8 | let minBuyPrice = prices[0]; 9 | /* 1. we start from the first index, as we can sell from the first day 10 | 2. we check if the previous value(buy price) is minimum. Because only then we can make max profit 11 | 12 | */ 13 | for(let sellDay = 1; sellDay < prices.length; sellDay++){ 14 | let sellPrice = prices[sellDay]; 15 | let profit = sellPrice - minBuyPrice; 16 | 17 | // min buy price 18 | minBuyPrice = Math.min(minBuyPrice, prices[sellDay]) 19 | 20 | // max profit 21 | maxProfit = Math.max(profit, maxProfit); 22 | 23 | } 24 | return maxProfit; 25 | 26 | }; -------------------------------------------------------------------------------- /Arrays/152_maximum_product_subarray.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var maxProduct = function(nums) { 6 | let currMaxProd = nums[0]; 7 | let currMinProd = nums[0]; 8 | let result = nums[0]; 9 | for(let i = 1; i< nums.length; i++){ 10 | let maxProd = Math.max(currMaxProd * nums[i], nums[i], currMinProd * nums[i]) 11 | let minProd = Math.min(currMaxProd * nums[i], nums[i], currMinProd * nums[i]) 12 | 13 | currMaxProd = Math.max(maxProd, minProd) 14 | currMinProd = Math.min(maxProd, minProd) 15 | 16 | result = Math.max(result, currMaxProd) 17 | } 18 | return result; 19 | 20 | }; -------------------------------------------------------------------------------- /Arrays/153_find_minimum_in_rotated_sorted_array.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var findMin = function(nums) { 6 | let low=0; 7 | let high=nums.length-1; 8 | 9 | while(low target 14 | right-- 15 | case 2: sum < target 16 | left++ 17 | case 3: sum === target 18 | 19 | 20 | */ 21 | var threeSum = function(nums) { 22 | let result = []; 23 | nums.sort((a,b) => a - b); 24 | 25 | for(let i = 0; i< nums.length; i++){ 26 | // check to ignore duplicate i values 27 | if(nums[i] !== nums[i-1]){ 28 | let left = i + 1; 29 | let right = nums.length - 1; 30 | let target = 0 - nums[i]; 31 | while(left < right){ 32 | let sum = nums[left] + nums[right]; 33 | if(sum > target){ 34 | right--; 35 | }else if(sum < target){ 36 | left++; 37 | }else{ 38 | // when sum === target, then push to result 39 | result.push([nums[i], nums[left], nums[right]]); 40 | // ignore duplicate values 41 | while(nums[left] === nums[left + 1]) left++; 42 | while(nums[right] === nums[right - 1]) right--; 43 | left++; 44 | right--; 45 | } 46 | } 47 | 48 | } 49 | } 50 | return result; 51 | }; -------------------------------------------------------------------------------- /Arrays/1_two_sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} target 4 | * @return {number[]} 5 | */ 6 | var twoSum = function(nums, target) { 7 | var map = {}; 8 | for(let i = 0; i< nums.length;i++){ 9 | let value = nums[i]; 10 | let complementPair = target - value; 11 | // if complementpair is present in map 12 | if(map[complementPair] !== undefined){ 13 | return [map[complementPair], i] 14 | }else{ 15 | map[value] = i; 16 | } 17 | } 18 | 19 | }; -------------------------------------------------------------------------------- /Arrays/217_contains_duplicate.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {boolean} 4 | */ 5 | var containsDuplicate = function(nums) { 6 | let map = {}; 7 | for(let i = 0; i-1;i--){ 22 | //push 1 at index 0 23 | if(rightProd.length === 0){ 24 | rightProd.push(1) 25 | }else{ 26 | rightProd.unshift((rightProd[0]*nums[i+1])) 27 | } 28 | } 29 | 30 | // populate solution 31 | for(let i = 0; i= nums[low] && target <= nums[mid]){ 16 | high = mid - 1 17 | }else{ 18 | low = mid + 1; 19 | } 20 | 21 | }else{ 22 | if(target >= nums[mid] && target <= nums[high]){ 23 | low = mid + 1; 24 | }else{ 25 | high = mid - 1; 26 | } 27 | 28 | } 29 | } 30 | return -1; 31 | 32 | }; -------------------------------------------------------------------------------- /Arrays/53_maximum_subarray.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var maxSubArray = function(nums) { 6 | let currSum = nums[0]; 7 | let maxSum = nums[0]; 8 | if(nums.length === 1) return nums[0]; 9 | for(let i = 1; i>= 1; // Right shift n by 1 to move to the next bit 11 | } 12 | return result >>> 0; // Unsigned right shift to handle negative numbers 13 | }; 14 | -------------------------------------------------------------------------------- /Binary/191_number_of_1_bits.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {number} 4 | */ 5 | var hammingWeight = function(n) { 6 | let count = 0; 7 | while (n != 0) { 8 | count += n & 1; // Add the least significant bit to count 9 | n >>>= 1; // Right shift n by 1 bit 10 | } 11 | return count; 12 | }; 13 | -------------------------------------------------------------------------------- /Binary/268_missing_number.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var missingNumber = function(nums) { 6 | const n = nums.length; 7 | const expectedSum = (n * (n + 1)) / 2; // Sum of first n natural numbers 8 | const actualSum = nums.reduce((acc, num) => acc + num, 0); // Sum of elements in nums 9 | return expectedSum - actualSum; 10 | }; 11 | -------------------------------------------------------------------------------- /Binary/338_counting_bits.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {number[]} 4 | */ 5 | var countBits = function(n) { 6 | const dp = new Array(n + 1).fill(0); 7 | 8 | for (let i = 1; i <= n; i++) { 9 | // To count the number of set bits in i, we use the formula: 10 | // dp[i] = dp[i & (i - 1)] + 1 11 | // Explanation: (i & (i - 1)) removes the least significant bit of i, 12 | // which is a power of 2. So, dp[i] = dp[i - 1] + 1 13 | dp[i] = dp[i & (i - 1)] + 1; 14 | } 15 | 16 | return dp; 17 | }; 18 | -------------------------------------------------------------------------------- /Binary/371_sum_of_two_integers.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} a 3 | * @param {number} b 4 | * @return {number} 5 | */ 6 | var getSum = function(a, b) { 7 | while (b != 0) { 8 | let carry = (a & b) << 1; 9 | a = a ^ b; 10 | b = carry; 11 | } 12 | return a; 13 | }; 14 | -------------------------------------------------------------------------------- /Heap/23_merge_k_sorted_lists.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val, next) { 4 | * this.val = (val===undefined ? 0 : val) 5 | * this.next = (next===undefined ? null : next) 6 | * } 7 | */ 8 | /** 9 | * @param {ListNode[]} lists 10 | * @return {ListNode} 11 | */ 12 | class ListNode { 13 | constructor(val = 0, next = null) { 14 | this.val = val; 15 | this.next = next; 16 | } 17 | } 18 | 19 | var mergeKLists = function(lists) { 20 | if (!lists || lists.length === 0) return null; 21 | 22 | const heap = new MinHeap(); 23 | 24 | // Push the head of each linked list to the heap 25 | for (let list of lists) { 26 | if (list) heap.push(list); 27 | } 28 | 29 | const dummyHead = new ListNode(); 30 | let current = dummyHead; 31 | 32 | // Merge lists by popping the minimum node from the heap 33 | while (!heap.isEmpty()) { 34 | const minNode = heap.pop(); 35 | current.next = minNode; 36 | current = current.next; 37 | if (minNode.next) heap.push(minNode.next); 38 | } 39 | 40 | return dummyHead.next; 41 | }; 42 | 43 | class MinHeap { 44 | constructor() { 45 | this.heap = []; 46 | } 47 | 48 | push(node) { 49 | this.heap.push(node); 50 | this.heapifyUp(); 51 | } 52 | 53 | pop() { 54 | if (this.isEmpty()) return null; 55 | const minNode = this.heap[0]; 56 | this.heap[0] = this.heap[this.heap.length - 1]; 57 | this.heap.pop(); 58 | this.heapifyDown(); 59 | return minNode; 60 | } 61 | 62 | isEmpty() { 63 | return this.heap.length === 0; 64 | } 65 | 66 | heapifyUp() { 67 | let currentIdx = this.heap.length - 1; 68 | while (currentIdx > 0) { 69 | const parentIdx = Math.floor((currentIdx - 1) / 2); 70 | if (this.heap[currentIdx].val < this.heap[parentIdx].val) { 71 | [this.heap[currentIdx], this.heap[parentIdx]] = [this.heap[parentIdx], this.heap[currentIdx]]; 72 | currentIdx = parentIdx; 73 | } else { 74 | break; 75 | } 76 | } 77 | } 78 | 79 | heapifyDown() { 80 | let currentIdx = 0; 81 | while (currentIdx < this.heap.length) { 82 | let minIdx = currentIdx; 83 | const leftChildIdx = currentIdx * 2 + 1; 84 | const rightChildIdx = currentIdx * 2 + 2; 85 | if (leftChildIdx < this.heap.length && this.heap[leftChildIdx].val < this.heap[minIdx].val) { 86 | minIdx = leftChildIdx; 87 | } 88 | if (rightChildIdx < this.heap.length && this.heap[rightChildIdx].val < this.heap[minIdx].val) { 89 | minIdx = rightChildIdx; 90 | } 91 | if (minIdx !== currentIdx) { 92 | [this.heap[currentIdx], this.heap[minIdx]] = [this.heap[minIdx], this.heap[currentIdx]]; 93 | currentIdx = minIdx; 94 | } else { 95 | break; 96 | } 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /Heap/295_find_median_from_data_streams.js: -------------------------------------------------------------------------------- 1 | class MedianFinder { 2 | constructor() { 3 | this.lowerHalf = new MaxHeap(); 4 | this.upperHalf = new MinHeap(); 5 | } 6 | 7 | addNum(num) { 8 | if (this.lowerHalf.isEmpty() || num <= this.lowerHalf.peek()) { 9 | this.lowerHalf.push(num); 10 | } else { 11 | this.upperHalf.push(num); 12 | } 13 | 14 | // Rebalance the heaps if necessary 15 | if (this.lowerHalf.size() > this.upperHalf.size() + 1) { 16 | this.upperHalf.push(this.lowerHalf.pop()); 17 | } else if (this.upperHalf.size() > this.lowerHalf.size()) { 18 | this.lowerHalf.push(this.upperHalf.pop()); 19 | } 20 | } 21 | 22 | findMedian() { 23 | if (this.lowerHalf.size() === this.upperHalf.size()) { 24 | return (this.lowerHalf.peek() + this.upperHalf.peek()) / 2; 25 | } else { 26 | return this.lowerHalf.peek(); 27 | } 28 | } 29 | } 30 | 31 | class MaxHeap { 32 | constructor() { 33 | this.heap = []; 34 | } 35 | 36 | push(value) { 37 | this.heap.push(value); 38 | this.heapifyUp(); 39 | } 40 | 41 | pop() { 42 | if (this.isEmpty()) return null; 43 | const maxElement = this.heap[0]; 44 | this.heap[0] = this.heap[this.heap.length - 1]; 45 | this.heap.pop(); 46 | this.heapifyDown(); 47 | return maxElement; 48 | } 49 | 50 | peek() { 51 | return this.isEmpty() ? null : this.heap[0]; 52 | } 53 | 54 | size() { 55 | return this.heap.length; 56 | } 57 | 58 | isEmpty() { 59 | return this.heap.length === 0; 60 | } 61 | 62 | heapifyUp() { 63 | let currentIdx = this.heap.length - 1; 64 | while (currentIdx > 0) { 65 | const parentIdx = Math.floor((currentIdx - 1) / 2); 66 | if (this.heap[currentIdx] > this.heap[parentIdx]) { 67 | [this.heap[currentIdx], this.heap[parentIdx]] = [this.heap[parentIdx], this.heap[currentIdx]]; 68 | currentIdx = parentIdx; 69 | } else { 70 | break; 71 | } 72 | } 73 | } 74 | 75 | heapifyDown() { 76 | let currentIdx = 0; 77 | while (currentIdx < this.heap.length) { 78 | let maxIdx = currentIdx; 79 | const leftChildIdx = currentIdx * 2 + 1; 80 | const rightChildIdx = currentIdx * 2 + 2; 81 | if (leftChildIdx < this.heap.length && this.heap[leftChildIdx] > this.heap[maxIdx]) { 82 | maxIdx = leftChildIdx; 83 | } 84 | if (rightChildIdx < this.heap.length && this.heap[rightChildIdx] > this.heap[maxIdx]) { 85 | maxIdx = rightChildIdx; 86 | } 87 | if (maxIdx !== currentIdx) { 88 | [this.heap[currentIdx], this.heap[maxIdx]] = [this.heap[maxIdx], this.heap[currentIdx]]; 89 | currentIdx = maxIdx; 90 | } else { 91 | break; 92 | } 93 | } 94 | } 95 | } 96 | 97 | class MinHeap { 98 | constructor() { 99 | this.heap = []; 100 | } 101 | 102 | push(value) { 103 | this.heap.push(value); 104 | this.heapifyUp(); 105 | } 106 | 107 | pop() { 108 | if (this.isEmpty()) return null; 109 | const minElement = this.heap[0]; 110 | this.heap[0] = this.heap[this.heap.length - 1]; 111 | this.heap.pop(); 112 | this.heapifyDown(); 113 | return minElement; 114 | } 115 | 116 | peek() { 117 | return this.isEmpty() ? null : this.heap[0]; 118 | } 119 | 120 | size() { 121 | return this.heap.length; 122 | } 123 | 124 | isEmpty() { 125 | return this.heap.length === 0; 126 | } 127 | 128 | heapifyUp() { 129 | let currentIdx = this.heap.length - 1; 130 | while (currentIdx > 0) { 131 | const parentIdx = Math.floor((currentIdx - 1) / 2); 132 | if (this.heap[currentIdx] < this.heap[parentIdx]) { 133 | [this.heap[currentIdx], this.heap[parentIdx]] = [this.heap[parentIdx], this.heap[currentIdx]]; 134 | currentIdx = parentIdx; 135 | } else { 136 | break; 137 | } 138 | } 139 | } 140 | 141 | heapifyDown() { 142 | let currentIdx = 0; 143 | while (currentIdx < this.heap.length) { 144 | let minIdx = currentIdx; 145 | const leftChildIdx = currentIdx * 2 + 1; 146 | const rightChildIdx = currentIdx * 2 + 2; 147 | if (leftChildIdx < this.heap.length && this.heap[leftChildIdx] < this.heap[minIdx]) { 148 | minIdx = leftChildIdx; 149 | } 150 | if (rightChildIdx < this.heap.length && this.heap[rightChildIdx] < this.heap[minIdx]) { 151 | minIdx = rightChildIdx; 152 | } 153 | if (minIdx !== currentIdx) { 154 | [this.heap[currentIdx], this.heap[minIdx]] = [this.heap[minIdx], this.heap[currentIdx]]; 155 | currentIdx = minIdx; 156 | } else { 157 | break; 158 | } 159 | } 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /Heap/347_top_k_frequent_elements.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} k 4 | * @return {number[]} 5 | */ 6 | var topKFrequent = function(nums, k) { 7 | // Step 1: Count the frequency of each element using a hashmap 8 | const freqMap = new Map(); 9 | for (let num of nums) { 10 | freqMap.set(num, (freqMap.get(num) || 0) + 1); 11 | } 12 | 13 | // Step 2: Create a min heap to store the k most frequent elements 14 | const minHeap = new MinHeap(); 15 | for (let [num, freq] of freqMap.entries()) { 16 | minHeap.push({ num, freq }); 17 | if (minHeap.size() > k) { 18 | minHeap.pop(); 19 | } 20 | } 21 | 22 | // Step 3: Extract the elements from the min heap 23 | const result = []; 24 | while (!minHeap.isEmpty()) { 25 | result.unshift(minHeap.pop().num); 26 | } 27 | 28 | return result; 29 | }; 30 | 31 | class MinHeap { 32 | constructor() { 33 | this.heap = []; 34 | } 35 | 36 | push(value) { 37 | this.heap.push(value); 38 | this.heapifyUp(); 39 | } 40 | 41 | pop() { 42 | if (this.isEmpty()) return null; 43 | const minElement = this.heap[0]; 44 | this.heap[0] = this.heap[this.heap.length - 1]; 45 | this.heap.pop(); 46 | this.heapifyDown(); 47 | return minElement; 48 | } 49 | 50 | size() { 51 | return this.heap.length; 52 | } 53 | 54 | isEmpty() { 55 | return this.heap.length === 0; 56 | } 57 | 58 | heapifyUp() { 59 | let currentIdx = this.heap.length - 1; 60 | while (currentIdx > 0) { 61 | const parentIdx = Math.floor((currentIdx - 1) / 2); 62 | if (this.heap[currentIdx].freq < this.heap[parentIdx].freq) { 63 | [this.heap[currentIdx], this.heap[parentIdx]] = [this.heap[parentIdx], this.heap[currentIdx]]; 64 | currentIdx = parentIdx; 65 | } else { 66 | break; 67 | } 68 | } 69 | } 70 | 71 | heapifyDown() { 72 | let currentIdx = 0; 73 | while (currentIdx < this.heap.length) { 74 | let minIdx = currentIdx; 75 | const leftChildIdx = currentIdx * 2 + 1; 76 | const rightChildIdx = currentIdx * 2 + 2; 77 | if (leftChildIdx < this.heap.length && this.heap[leftChildIdx].freq < this.heap[minIdx].freq) { 78 | minIdx = leftChildIdx; 79 | } 80 | if (rightChildIdx < this.heap.length && this.heap[rightChildIdx].freq < this.heap[minIdx].freq) { 81 | minIdx = rightChildIdx; 82 | } 83 | if (minIdx !== currentIdx) { 84 | [this.heap[currentIdx], this.heap[minIdx]] = [this.heap[minIdx], this.heap[currentIdx]]; 85 | currentIdx = minIdx; 86 | } else { 87 | break; 88 | } 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /Matrix/48_rotate_image.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} matrix 3 | * @return {void} Do not return anything, modify matrix in-place instead. 4 | */ 5 | var rotate = function (matrix) { 6 | const n = matrix.length; 7 | 8 | // Step 1: Transpose the matrix 9 | for (let i = 0; i < n; i++) { 10 | for (let j = i + 1; j < n; j++) { 11 | [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]]; // Swap elements 12 | } 13 | } 14 | 15 | // Step 2: Reverse each row of the transposed matrix 16 | for (let i = 0; i < n; i++) { 17 | let left = 0, 18 | right = n - 1; 19 | while (left < right) { 20 | [matrix[i][left], matrix[i][right]] = [matrix[i][right], matrix[i][left]]; // Swap elements 21 | left++; 22 | right--; 23 | } 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Matrix/54_spiral_matrix.js: -------------------------------------------------------------------------------- 1 | var spiralOrder = function (matrix) { 2 | const result = []; 3 | let top = 0, 4 | bottom = matrix.length - 1; 5 | let left = 0, 6 | right = matrix[0].length - 1; 7 | 8 | while (top <= bottom && left <= right) { 9 | // Traverse top row from left to right 10 | for (let i = left; i <= right; i++) { 11 | result.push(matrix[top][i]); 12 | } 13 | top++; 14 | 15 | // Traverse right column from top to bottom 16 | for (let i = top; i <= bottom; i++) { 17 | result.push(matrix[i][right]); 18 | } 19 | right--; 20 | 21 | // Check if top has crossed bottom or left has crossed right 22 | if (top <= bottom && left <= right) { 23 | // Traverse bottom row from right to left 24 | for (let i = right; i >= left; i--) { 25 | result.push(matrix[bottom][i]); 26 | } 27 | bottom--; 28 | 29 | // Traverse left column from bottom to top 30 | for (let i = bottom; i >= top; i--) { 31 | result.push(matrix[i][left]); 32 | } 33 | left++; 34 | } 35 | } 36 | 37 | return result; 38 | }; 39 | -------------------------------------------------------------------------------- /Matrix/73_set_matrix_zeroes.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} matrix 3 | * @return {void} Do not return anything, modify matrix in-place instead. 4 | */ 5 | var setZeroes = function (matrix) { 6 | const m = matrix.length; 7 | const n = matrix[0].length; 8 | 9 | let firstRowHasZero = false; 10 | let firstColHasZero = false; 11 | 12 | // Check if first row has zero 13 | for (let j = 0; j < n; j++) { 14 | if (matrix[0][j] === 0) { 15 | firstRowHasZero = true; 16 | break; 17 | } 18 | } 19 | 20 | // Check if first column has zero 21 | for (let i = 0; i < m; i++) { 22 | if (matrix[i][0] === 0) { 23 | firstColHasZero = true; 24 | break; 25 | } 26 | } 27 | 28 | // Mark first row and first column based on rest of the matrix 29 | for (let i = 1; i < m; i++) { 30 | for (let j = 1; j < n; j++) { 31 | if (matrix[i][j] === 0) { 32 | matrix[i][0] = 0; 33 | matrix[0][j] = 0; 34 | } 35 | } 36 | } 37 | 38 | // Set zeroes based on marks in first row and first column 39 | for (let i = 1; i < m; i++) { 40 | for (let j = 1; j < n; j++) { 41 | if (matrix[i][0] === 0 || matrix[0][j] === 0) { 42 | matrix[i][j] = 0; 43 | } 44 | } 45 | } 46 | 47 | // Set zeroes for first row and first column if needed 48 | if (firstRowHasZero) { 49 | for (let j = 0; j < n; j++) { 50 | matrix[0][j] = 0; 51 | } 52 | } 53 | 54 | if (firstColHasZero) { 55 | for (let i = 0; i < m; i++) { 56 | matrix[i][0] = 0; 57 | } 58 | } 59 | }; 60 | -------------------------------------------------------------------------------- /Matrix/79_word_search.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {character[][]} board 3 | * @param {string} word 4 | * @return {boolean} 5 | */ 6 | var exist = function (board, word) { 7 | const m = board.length; 8 | const n = board[0].length; 9 | 10 | // Helper function to perform DFS search 11 | function dfs(i, j, index) { 12 | if (index === word.length) return true; 13 | if (i < 0 || j < 0 || i >= m || j >= n || board[i][j] !== word[index]) 14 | return false; 15 | 16 | const temp = board[i][j]; // Temporarily mark current cell as visited 17 | board[i][j] = "#"; // Mark visited 18 | 19 | // Explore adjacent cells 20 | const found = 21 | dfs(i + 1, j, index + 1) || 22 | dfs(i - 1, j, index + 1) || 23 | dfs(i, j + 1, index + 1) || 24 | dfs(i, j - 1, index + 1); 25 | 26 | board[i][j] = temp; // Restore cell 27 | 28 | return found; 29 | } 30 | 31 | // Iterate through each cell in the board 32 | for (let i = 0; i < m; i++) { 33 | for (let j = 0; j < n; j++) { 34 | if (dfs(i, j, 0)) return true; // If word found, return true 35 | } 36 | } 37 | 38 | return false; // Word not found 39 | }; 40 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Blind-75 Leetcode Solution In JS 🔥 2 | This repo contains solution for [Blind 75 Leetcode](https://leetcode.com/discuss/general-discussion/460599/blind-75-leetcode-questions) questions solved using Javascript. 3 | 4 | 5 | | Problem | Solution | Category | Difficulty | 6 | | --------- | -------- | --------- | ---------- | 7 | | [Two Sum](https://leetcode.com/problems/two-sum/) |[Solution](./Arrays/1_two_sum.js) | `Array` | Easy | 8 | | [Best Time to Buy and Sell Stocks](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [Solution](./Arrays/121_best_time_to_buy_and_sell_stocks.js) | `Array` | Easy | 9 | | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [Solution](./Arrays/217_contains_duplicate.js) | `Array` | Easy | 10 | | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [Solution](./Arrays/238_product_of_array_except_self.js) | `Array` | Medium | 11 | | [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | [Solution](./Arrays/53_maximum_subarray.js) | `Array` | Medium | 12 | | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [Solution](./Arrays/152_maximum_product_subarray.js) | `Array` | Medium | 13 | | [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | [Solution](./Arrays/153_find_minimum_in_rotated_sorted_array.js) | `Array` | Medium | 14 | | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [Solution](./Arrays/33_search_in_rotated_sorted_array.js) | `Array` | Medium | 15 | | [3Sum](https://leetcode.com/problems/3sum/) | [Solution](./Arrays/15_3Sum.js) | `Array` | Medium | 16 | | [Container With Water](https://leetcode.com/problems/container-with-most-water/) | [Solution](./Arrays/11_container_with_most_water.js) | `Array` | Medium | 17 | | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Solution](./Strings/3_longest_substring_with_repeating_character.js) | `String` | Medium | 18 | | [Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/) | [Solution](./Strings/424_longest_repeating_character_replacement.js) | `String` | Medium | 19 | | [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/) | [Solution](./Strings/76_minimum_window_substring.js) | `String` | Hard | 20 | | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Solution](./Strings/242_valid_anagram.js) | `String` | Easy | 21 | | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [Solution](./Strings/49_group_anagrams.js) | `String` | Medium | 22 | | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Solution](./Strings/20_valid_parentheses.js) | `String` | Easy | 23 | | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](./Strings//125_valid_palindrome.js) | `String` | Easy | 24 | | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) | [Solution](./Strings/5_longest_palandromic_substring.js) | `String` | Medium | 25 | | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [Solution](./Strings/647_palindromic_substrings.js) | `String` | Medium | 26 | | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [Solution](./Matrix/54_spiral_matrix.js) | `Matrix` | Medium | 27 | | [Set Matrix zeroes](https://leetcode.com/problems/set-matrix-zeroes/) | [Solution](./Matrix/73_set_matrix_zeroes.js) | `Matrix` | Medium | 28 | | [Rotate Image](https://leetcode.com/problems/rotate-image/) | [Solution](./Matrix/48_rotate_image.js) | `Matrix` | Medium | 29 | | [Word Search](https://leetcode.com/problems/word-search/) | [Solution](./Matrix/79_word_search.js) | `Matrix` | Medium | 30 | | [Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [Solution](./Binary/190_reverse_bits.js) | `Binary` | Easy | 31 | | [Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [Solution](./Binary/191_number_of_1_bits.js) | `Binary` | Easy | 32 | | [Missing Numbers](https://leetcode.com/problems/missing-numbers/) | [Solution](./Binary/268_missing_number.js) | `Binary` | Easy | 33 | | [Counting Bits](https://leetcode.com/problems/counting-bits/) | [Solution](./Binary/338_counting_bits.js) | `Binary` | Easy | 34 | | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [Solution](./Binary/371_sum_of_two_integers.js) | `Binary` | Medium | 35 | | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [Solution](./Heap/347_top_k_frequent_elements.js) | `Heap` | Medium | 36 | | [Merge K Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) | [Solution](./Heap/23_merge_k_sorted_lists.js) | `Heap` | Hard | 37 | | [Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/) | [Solution](./Heap/295_find_median_from_data_streams.js) | `Heap` | Hard | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Strings/125_valid_palindrome.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} s 3 | * @return {boolean} 4 | */ 5 | var isPalindrome = function(s) { 6 | s= s.replace(/[^a-z0-9]/gi, "") 7 | let start = 0; 8 | let end = s.length - 1; 9 | while(start < end){ 10 | if(s[start].toLowerCase() != s[end].toLowerCase()) return false; 11 | start++; 12 | end--; 13 | } 14 | return true; 15 | 16 | }; -------------------------------------------------------------------------------- /Strings/20_valid_parentheses.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} s 3 | * @return {boolean} 4 | */ 5 | var isValid = function(s) { 6 | let hashmap = { "(" : ")", "{" : "}", "[" : "]"} 7 | let stack = []; 8 | for(let ch of s){ 9 | if(hashmap[ch]){ 10 | // ch is opening brackets 11 | stack.push(hashmap[ch]) 12 | }else if(stack.length > 0 && stack[stack.length - 1] === ch){ 13 | // ch is closing brackets and top stack matches 14 | stack.pop() 15 | }else{ 16 | // ch is closing bracket and top stack doesn't match 17 | return false; 18 | } 19 | } 20 | return stack.length === 0; 21 | }; -------------------------------------------------------------------------------- /Strings/242_valid_anagram.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} s 3 | * @param {string} t 4 | * @return {boolean} 5 | */ 6 | var isAnagram = function(s, t) { 7 | let count = {}; 8 | if( s.length !== t.length) return false; 9 | for(let i = 0; i< s.length; i++){ 10 | // add elements if not already present in map 11 | if(!count[s[i]]) count[s[i]] = 0; 12 | if(!count[t[i]]) count[t[i]] = 0; 13 | count[s[i]]++; 14 | count[t[i]]--; 15 | } 16 | for(let ch in count){ 17 | if(count[ch] !== 0) return false; 18 | } 19 | return true; 20 | 21 | }; -------------------------------------------------------------------------------- /Strings/3_longest_substring_with_repeating_character.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} s 3 | * @return {number} 4 | */ 5 | var lengthOfLongestSubstring = function(s) { 6 | 7 | let max = 0; 8 | let begin = 0; 9 | let set = new Set(); 10 | for(let end = 0; end < s.length; end++){ 11 | // if end element already present in set 12 | while(set.has(s[end])){ 13 | // delete all elements till repeated element 14 | set.delete(s[begin]); 15 | begin++; 16 | } 17 | set.add(s[end]); 18 | max = Math.max(max, set.size); 19 | 20 | } 21 | return max; 22 | 23 | }; -------------------------------------------------------------------------------- /Strings/424_longest_repeating_character_replacement.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} s 3 | * @param {number} k 4 | * @return {number} 5 | */ 6 | var characterReplacement = function (s, k) { 7 | let start = 0; 8 | let frequencyMap = {}; 9 | let maxFreq = 0; 10 | let longestSubstringLength = 0; 11 | for (let end = 0; end < s.length; end++) { 12 | // if element not present in map, then initialize it with 0 13 | if (!frequencyMap[s[end]]) { 14 | frequencyMap[s[end]] = 0; 15 | } 16 | frequencyMap[s[end]]++; 17 | 18 | maxFreq = Math.max(maxFreq, frequencyMap[s[end]]); 19 | let isValid = end - start + 1 - maxFreq <= k; 20 | if (!isValid) { 21 | // decrease frequency of the start element in frequency map 22 | frequencyMap[s[start]]--; 23 | start++; 24 | } 25 | 26 | longestSubstringLength = end - start + 1; 27 | } 28 | return longestSubstringLength; 29 | }; 30 | -------------------------------------------------------------------------------- /Strings/49_group_anagrams.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string[]} strs 3 | * @return {string[][]} 4 | */ 5 | var groupAnagrams = function(strs) { 6 | let sortedStrs = strs.map(word => word.split('').sort().join('')); 7 | let hash = {}; 8 | for(let i = 0; i< strs.length; i++){ 9 | if(!hash[sortedStrs[i]]){ 10 | hash[sortedStrs[i]] = [strs[i]]; 11 | }else{ 12 | hash[sortedStrs[i]].push(strs[i]) 13 | } 14 | } 15 | return Object.values(hash) 16 | 17 | }; -------------------------------------------------------------------------------- /Strings/5_longest_palandromic_substring.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} s 3 | * @return {string} 4 | */ 5 | var longestPalindrome = function(s) { 6 | let start = 0; 7 | let low, high; 8 | let maxLength = 0; 9 | if(s.length < 2) return s; 10 | for(let i = 0; i< s.length; i+=0.5){ 11 | low = Math.floor(i); 12 | high = Math.ceil(i); 13 | while(low >= 0 && high < s.length && s[low] === s[high]){ 14 | low--; 15 | high++; 16 | } 17 | let length = high - low - 1; 18 | if(length > maxLength){ 19 | maxLength = length; 20 | start = low + 1; 21 | } 22 | 23 | } 24 | return s.substring(start, start + maxLength) 25 | 26 | }; -------------------------------------------------------------------------------- /Strings/647_palindromic_substrings.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} s 3 | * @return {number} 4 | */ 5 | var countSubstrings = function (s) { 6 | let result = 0; 7 | for (let i = 0; i < s.length; i += 0.5) { 8 | let left = Math.floor(i); 9 | let right = Math.ceil(i); 10 | while (left >= 0 && right < s.length) { 11 | if (s[left] === s[right]) { 12 | result++; 13 | } else { 14 | break; 15 | } 16 | left--; 17 | right++; 18 | } 19 | } 20 | return result; 21 | }; 22 | --------------------------------------------------------------------------------