-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindLongestSubstring.js
More file actions
32 lines (26 loc) · 748 Bytes
/
findLongestSubstring.js
File metadata and controls
32 lines (26 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// findLongestSubstring with distinct characters
// set longest substring to 0;
// create map for letters;
// if same letter is encountered twice
// reset streak and letter count
// function findLongestSubstring(word) {
// let longest = 0;
// let seen = {};
// let streak = 0;
// for (let letter of word) {
// if (!seen[letter]) {
// seen[letter] = 1;
// streak++;
// } else {
// seen = {};
// if (streak > longest) {
// longest = streak;
// }
// streak = 0;
// }
// }
// return longest > streak ? longest : streak;
// }
console.log(findLongestSubstring("")); //0
console.log(findLongestSubstring("rithmschool")); //7
console.log(findLongestSubstring("thisisawesome")); //6