File tree 2 files changed +25
-1
lines changed
2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change 7
7
* .vscode /
8
8
src /main /java /com /fishercoder /solutions /_99999RandomQuestions.java
9
9
src /main /java /com /fishercoder /solutions /_Contest.java
10
- .project
10
+ .project
11
+ bin
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments