-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind-longest-string.js
More file actions
49 lines (34 loc) · 1.09 KB
/
find-longest-string.js
File metadata and controls
49 lines (34 loc) · 1.09 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// // learning sliding window pattern algorithm.
// // this function will return the longest substring with all distinct characters.
// // if the string is empty or not provided return O.
function findLongestSubstring(str){
// add whatever parameters you deem necessary - good luck!
if(typeof(str) !== "string" || str.length === 0) return 0;
let longest = 0;
let seen = {};
let start = 0;
for (let i = 0; i < str.length; i++) {
let char = str[i];
if (seen[char]) {
start = Math.max(start, seen[char]);
}
longest = Math.max(longest, i - start + 1);
seen[char] = i + 1;
}
console.log(seen);
return longest;
}
console.log(findLongestSubstring("abcabcbb"));
// const countDown = (num) => (num === 1) ? 1 : num * countDown(num - 1);
// console.log(countDown(5))
// function collectOdds(arr){
// let newArr = [];
// if(arr.length === 0){
// return newArr;
// }
// if(arr[0] % 2 !== 0){
// newArr.push(arr[0])
// }
// return newArr.concat(collectOdds(arr.slice(1)));
// }
// console.log(collectOdds([1,2,3,4,5]))