From 30ac3dbea0981e79551bb181371f9f202c78c576 Mon Sep 17 00:00:00 2001 From: sambabib Date: Mon, 23 Sep 2024 11:23:31 +0100 Subject: [PATCH 1/4] added js solution to _3 --- .gitignore | 3 ++- javascript/_3.js | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 javascript/_3.js 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/_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 From 9447151eb5253e8e0c1bb8ad861a818c5bf5fdea Mon Sep 17 00:00:00 2001 From: sambabib Date: Wed, 9 Oct 2024 14:31:50 +0100 Subject: [PATCH 2/4] js solution to _17 --- javascript/_17.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 javascript/_17.js 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; +}; From 4ca41ba941da14fc113cd4e4f2808b9d4704e9e7 Mon Sep 17 00:00:00 2001 From: sambabib Date: Tue, 15 Oct 2024 10:38:01 +0100 Subject: [PATCH 3/4] added js solution links to readme --- paginated_contents/algorithms/1st_thousand/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paginated_contents/algorithms/1st_thousand/README.md b/paginated_contents/algorithms/1st_thousand/README.md index d9601619b0..28bfa81295 100644 --- a/paginated_contents/algorithms/1st_thousand/README.md +++ b/paginated_contents/algorithms/1st_thousand/README.md @@ -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/sambabib/Leetcode/blob/sambabib-js-solutions/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/sambabib/Leetcode/blob/sambabib-js-solutions/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 From daed7dc3b148d9fea4687a354ba047a61e7e4379 Mon Sep 17 00:00:00 2001 From: sambabib Date: Wed, 16 Oct 2024 09:53:00 +0100 Subject: [PATCH 4/4] updated links to js solutions --- paginated_contents/algorithms/1st_thousand/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paginated_contents/algorithms/1st_thousand/README.md b/paginated_contents/algorithms/1st_thousand/README.md index 28bfa81295..24e367d524 100644 --- a/paginated_contents/algorithms/1st_thousand/README.md +++ b/paginated_contents/algorithms/1st_thousand/README.md @@ -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), [Javascript](https://github.com/sambabib/Leetcode/blob/sambabib-js-solutions/javascript/_17.js) || 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), [Javascript](https://github.com/sambabib/Leetcode/blob/sambabib-js-solutions/javascript/_3.js) | | 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