-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubstring.ts
More file actions
33 lines (32 loc) · 933 Bytes
/
substring.ts
File metadata and controls
33 lines (32 loc) · 933 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
33
/**
*
* A function which returns the length of longest substring.
* Which takes a String as input.
*
* @param {string} inputString - Input String
* @returns Length of the Longest Substring
*/
function lengthOfLongestSubstring(inputString: string): number {
if (inputString.length === 0) {
return 0;
}
let subString = new Set<string>();
let start = 0,
end = 0,
maxLength = 0;
while (start < inputString.length && end < inputString.length) {
if (!subString.has(inputString[end])) {
subString.add(inputString[end]);
end++;
maxLength = Math.max(maxLength, end - start);
} else {
subString.delete(inputString[start]);
start++;
}
}
return maxLength;
}
console.log(lengthOfLongestSubstring("abcabcbb"));
console.log(lengthOfLongestSubstring("bbbbb"));
console.log(lengthOfLongestSubstring("pwwkew"));
console.log(lengthOfLongestSubstring("javaconceptoftheday"));