Skip to content

Commit edb6e8c

Browse files
committed
Added longest substring solution
1 parent 797b86b commit edb6e8c

File tree

1 file changed

+19
-0
lines changed
  • longest-substring-without-repeating-characters

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var lengthOfLongestSubstring = function (s) {
2+
// Create map to store character from string
3+
let map = new Map();
4+
let maxLen = 0;
5+
let start = 0;
6+
7+
for (let end = 0; end < s.length; end++) {
8+
if (map.has(s[end])) {
9+
// Move the start pointer to the right of the previous position of current character
10+
start = Math.max(map.get(s[end]) + 1, start);
11+
}
12+
map.set(s[end], end);
13+
maxLen = Math.max(maxLen, end - start + 1);
14+
}
15+
return maxLen;
16+
};
17+
18+
// TC: O(n)
19+
// SC: O(n)

0 commit comments

Comments
 (0)