-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongestSubstring.ts
54 lines (47 loc) · 1.4 KB
/
longestSubstring.ts
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
50
51
52
53
54
/**
* @param {string} s
* @return {number}
*/
export function lengthOfLongestSubstring(s: string) {
if (s.length <= 1) {
return s;
}
let split = s.split('');
let temp = [];
let longest = '';
let word = s;
for (var i = 0; i < split.length; i++) {
if (temp.indexOf(split[i]) === -1) {
temp.push(split[i]);
} else {
let st;
if (temp.length > word.length) {
return temp.join('');
}
if (i > 0 && split[i-1] === split[i] && (i-1) !== 0 && (i+1) < split.length) {
st = word.substring(i+1);
} else {
// concat previous characters and pass along
let x = word.substring(0, i);
x = x.replace(split[i], '');
st = x + word.substring(i);
}
console.log('substring', st);
let ss = lengthOfLongestSubstring(st);
console.log('ss', ss);
console.log('temp', temp);
if (temp.join('').length >= ss.length) {
longest = temp.join('');
} else {
longest = ss;
temp = ss.split('');
}
break;
}
}
if (temp.join('').length >= longest.length) {
longest = temp.join('');
}
console.log('longest', longest);
return longest;
}