diff --git a/.gitignore b/.gitignore index b7dfd6457a..c6b4f54e91 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ out/ *.vscode/ src/main/java/com/fishercoder/solutions/_99999RandomQuestions.java src/main/java/com/fishercoder/solutions/_Contest.java -.project \ No newline at end of file +.project +bin \ No newline at end of file diff --git a/javascript/_17.js b/javascript/_17.js new file mode 100644 index 0000000000..f950e696de --- /dev/null +++ b/javascript/_17.js @@ -0,0 +1,43 @@ +function letterCombinations(digits) { + // If the input is an empty string, return an empty array. + if (digits.length === 0) { + return []; + } + + // Mapping of digits to letters as per the telephone keypad using a javascript dictionary. + const digitToChar = { + '2': ['a', 'b', 'c'], + '3': ['d', 'e', 'f'], + '4': ['g', 'h', 'i'], + '5': ['j', 'k', 'l'], + '6': ['m', 'n', 'o'], + '7': ['p', 'q', 'r', 's'], + '8': ['t', 'u', 'v'], + '9': ['w', 'x', 'y', 'z'] + }; + + // Resultant array to store all possible combinations + const result = []; + + // Backtracking function to generate combinations + function backtrack(index, currentCombination) { + // if the current combination has the same length as the input digits. + if (index === digits.length) { + result.push(currentCombination); + return; + } + + // Get the letters that the current digit maps to. + let letters = digitToChar[digits[index]]; + + // Loop through the letters and call backtrack recursively for the next digit. + for (let letter of letters) { + backtrack(index + 1, currentCombination + letter); + } + } + + // Start backtracking from the first digit (index 0) with an empty string as the initial combination. + backtrack(0, ''); + + return result; +}; diff --git a/javascript/_3.js b/javascript/_3.js new file mode 100644 index 0000000000..2218e25361 --- /dev/null +++ b/javascript/_3.js @@ -0,0 +1,23 @@ +function lengthOfLongestSubstring(s) { + // Using the "sliding window" data structure. + // Create a javascript set to store unique characters. + let charSet = new Set(); + let left = 0; // Left pointer of the sliding window. + let maxLength = 0; + + // This moves the right pointer of the sliding window. + for (let right = 0; right < s.length; right++) { + // If the character at the right pointer is already in the set, move the left pointer. + while (charSet.has(s[right])) { + charSet.delete(s[left]); + left++; + } + // Add the current character at the right pointer to the set. + charSet.add(s[right]); + + // Update the maximum length of substring without repeating characters. + maxLength = Math.max(maxLength, right - left + 1); + } + + return maxLength; +} \ No newline at end of file diff --git a/javascript/_994.js b/javascript/_994.js new file mode 100644 index 0000000000..67ab649ddd --- /dev/null +++ b/javascript/_994.js @@ -0,0 +1,57 @@ +function orangesRotting(grid) { + const rows = grid.length; + const cols = grid[0].length; + let queue = []; + let freshOranges = 0; + + // Initialize the queue with all rotten oranges and count fresh oranges + for (let r = 0; r < rows; r++) { + for (let c = 0; c < cols; c++) { + if (grid[r][c] === 2) { + queue.push([r, c]); + } else if (grid[r][c] === 1) { + freshOranges++; + } + } + } + + // If there are no fresh oranges, return 0 + if (freshOranges === 0) return 0; + + let minutes = 0; + const directions = [ + [0, 1], // right + [1, 0], // down + [0, -1], // left + [-1, 0] // up + ]; + + // Step 2: Perform BFS + while (queue.length > 0) { + let size = queue.length; + let newRotten = false; + + for (let i = 0; i < size; i++) { + let [x, y] = queue.shift(); + + for (let [dx, dy] of directions) { + let nx = x + dx; + let ny = y + dy; + + // Check if the neighboring cell is a fresh orange + if (nx >= 0 && ny >= 0 && nx < rows && ny < cols && grid[nx][ny] === 1) { + grid[nx][ny] = 2; // Make it rotten + freshOranges--; // Decrease count of fresh oranges + queue.push([nx, ny]); // Add it to the queue + newRotten = true; + } + } + } + + // If rotten oranges exist, increment minutes + if (newRotten) minutes++; + } + + // Check if there are any fresh oranges left + return freshOranges === 0 ? minutes : -1; +} diff --git a/paginated_contents/algorithms/1st_thousand/README.md b/paginated_contents/algorithms/1st_thousand/README.md index d9601619b0..2979301b84 100644 --- a/paginated_contents/algorithms/1st_thousand/README.md +++ b/paginated_contents/algorithms/1st_thousand/README.md @@ -4,7 +4,7 @@ | 991 | [Broken Calculator](https://leetcode.com/problems/broken-calculator/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_991.java) | | Medium | Math, Greedy | 981 | [Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_981.java) | [:tv:](https://youtu.be/eVi4gDimCoo) | Medium | | 997 | [Find the Town Judge](https://leetcode.com/problems/find-the-town-judge/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_997.java) | | Easy | -| 994 | [Rotting Oranges](https://leetcode.com/problems/rotting-oranges/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_994.java) | | Medium | BFS +| 994 | [Rotting Oranges](https://leetcode.com/problems/rotting-oranges/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_994.java) [Javascript](https://github.com/fishercoder1534/Leetcode/blob/master/javascript/_994.js) | | Medium | BFS | 993 | [Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_993.java) | | Easy | Tree, BFS | 989 | [Add to Array-Form of Integer](https://leetcode.com/problems/add-to-array-form-of-integer/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_989.java) | | Easy | Array | 988 | [Smallest String Starting From Leaf](https://leetcode.com/problems/smallest-string-starting-from-leaf/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_988.java) | | Medium | Tree, DFS @@ -796,7 +796,7 @@ | 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_20.java) | [:tv:](https://www.youtube.com/watch?v=eBbg5pnq5Zg) | Easy | Stack | 19 | [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_19.java) | [:tv:](https://youtu.be/Kka8VgyFZfc) | Medium | Linked List | 18 | [4 Sum](https://leetcode.com/problems/4sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_18.java) || Medium | Two Pointers -| 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_17.java) || Medium | Backtracking +| 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_17.java), [Javascript](https://github.com/fishercoder1534/Leetcode/blob/master/javascript/_17.js) || Medium | Backtracking | 16 | [3Sum Closest](https://leetcode.com/problems/3sum-closest/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_16.java) || Medium | Two Pointers | 15 | [3Sum](https://leetcode.com/problems/3sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_15.java), [C++](../master/cpp/_15.cpp) | [:tv:](https://www.youtube.com/watch?v=jeim_j8VdiM) | Medium | Two Pointers, Binary Search | 14 | [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_14.java) | [:tv:](https://www.youtube.com/watch?v=K1ps6d7YCy4) | Easy @@ -810,6 +810,6 @@ | 6 | [ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_6.java) | | Easy | | 5 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_5.java) | | Medium | | 4 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_4.java), [C++](../master/cpp/_4.cpp) | | Hard | Divide and Conquer -| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_3.java), [C++](../master/cpp/_3.cpp) | | Medium | HashMap, Sliding Window +| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_3.java), [C++](../master/cpp/_3.cpp), [Javascript](https://github.com/fishercoder1534/Leetcode/blob/master/javascript/_3.js) | | Medium | HashMap, Sliding Window | 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_2.java) | | Medium | LinkedList | 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_1.java), [C++](../master/cpp/_1.cpp), [Javascript](../master/javascript/_1.js) | [:tv:](https://www.youtube.com/watch?v=kPXOr6pW8KM&t=) | Easy | HashMap \ No newline at end of file