Skip to content

Commit 2548c18

Browse files
authored
added js solution to _3 (#188)
* added js solution to _3 * js solution to _17 * added js solution links to readme * updated links to js solutions --------- Co-authored-by: sambabib <adekyte.gmail.com>
1 parent 9782608 commit 2548c18

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ out/
77
*.vscode/
88
src/main/java/com/fishercoder/solutions/_99999RandomQuestions.java
99
src/main/java/com/fishercoder/solutions/_Contest.java
10-
.project
10+
.project
11+
bin

javascript/_17.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function letterCombinations(digits) {
2+
// If the input is an empty string, return an empty array.
3+
if (digits.length === 0) {
4+
return [];
5+
}
6+
7+
// Mapping of digits to letters as per the telephone keypad using a javascript dictionary.
8+
const digitToChar = {
9+
'2': ['a', 'b', 'c'],
10+
'3': ['d', 'e', 'f'],
11+
'4': ['g', 'h', 'i'],
12+
'5': ['j', 'k', 'l'],
13+
'6': ['m', 'n', 'o'],
14+
'7': ['p', 'q', 'r', 's'],
15+
'8': ['t', 'u', 'v'],
16+
'9': ['w', 'x', 'y', 'z']
17+
};
18+
19+
// Resultant array to store all possible combinations
20+
const result = [];
21+
22+
// Backtracking function to generate combinations
23+
function backtrack(index, currentCombination) {
24+
// if the current combination has the same length as the input digits.
25+
if (index === digits.length) {
26+
result.push(currentCombination);
27+
return;
28+
}
29+
30+
// Get the letters that the current digit maps to.
31+
let letters = digitToChar[digits[index]];
32+
33+
// Loop through the letters and call backtrack recursively for the next digit.
34+
for (let letter of letters) {
35+
backtrack(index + 1, currentCombination + letter);
36+
}
37+
}
38+
39+
// Start backtracking from the first digit (index 0) with an empty string as the initial combination.
40+
backtrack(0, '');
41+
42+
return result;
43+
};

javascript/_3.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function lengthOfLongestSubstring(s) {
2+
// Using the "sliding window" data structure.
3+
// Create a javascript set to store unique characters.
4+
let charSet = new Set();
5+
let left = 0; // Left pointer of the sliding window.
6+
let maxLength = 0;
7+
8+
// This moves the right pointer of the sliding window.
9+
for (let right = 0; right < s.length; right++) {
10+
// If the character at the right pointer is already in the set, move the left pointer.
11+
while (charSet.has(s[right])) {
12+
charSet.delete(s[left]);
13+
left++;
14+
}
15+
// Add the current character at the right pointer to the set.
16+
charSet.add(s[right]);
17+
18+
// Update the maximum length of substring without repeating characters.
19+
maxLength = Math.max(maxLength, right - left + 1);
20+
}
21+
22+
return maxLength;
23+
}

paginated_contents/algorithms/1st_thousand/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@
796796
| 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
797797
| 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
798798
| 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
799-
| 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
799+
| 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
800800
| 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
801801
| 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
802802
| 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 @@
810810
| 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 |
811811
| 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 |
812812
| 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
813-
| 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
813+
| 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
814814
| 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
815815
| 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

0 commit comments

Comments
 (0)