Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ out/
*.vscode/
src/main/java/com/fishercoder/solutions/_99999RandomQuestions.java
src/main/java/com/fishercoder/solutions/_Contest.java
.project
.project
bin
43 changes: 43 additions & 0 deletions javascript/_17.js
Original file line number Diff line number Diff line change
@@ -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;
};
23 changes: 23 additions & 0 deletions javascript/_3.js
Original file line number Diff line number Diff line change
@@ -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;
}