Skip to content

Commit 30ac3db

Browse files
author
sambabib
committed
added js solution to _3
1 parent bde6a61 commit 30ac3db

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Diff for: .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

Diff for: 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+
}

0 commit comments

Comments
 (0)