- Notifications
You must be signed in to change notification settings - Fork1.3k
added js solution to _3#188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
fishercoder1534 merged 4 commits intofishercoder1534:masterfromsambabib:sambabib-js-solutionsNov 7, 2024
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
3 changes: 2 additions & 1 deletion.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletionsjavascript/_17.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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 deletionsjavascript/_3.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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; | ||
} |
4 changes: 2 additions & 2 deletionspaginated_contents/algorithms/1st_thousand/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.