Implement longestNonRepeatingSubstring(s). Given a string s, return the length (a number) of the longest contiguous substring of s that contains no duplicate characters. A substring is a contiguous slice of the original string — not a subsequence, which can skip characters. For "abcabcbb" the answer is 3 (the substring "abc"); for "bbbbb" it is 1 ("b"); for "pwwkew" it is 3 ("wke", not the 4-character subsequence "pwke").
This is the classic "longest substring without repeating characters" problem. The naive O(n²) solution is straightforward — and slow on long strings. The interesting work is doing it in O(n) with a sliding window plus a last-seen index map.
// s: string — any string (may be empty, may contain unicode)
// returns: number — length of the longest substring with no repeats
function longestNonRepeatingSubstring(s: string): number;
longestNonRepeatingSubstring('abcabcbb'); // → 3 ("abc")
longestNonRepeatingSubstring('bbbbb'); // → 1 ("b")
longestNonRepeatingSubstring('pwwkew'); // → 3 ("wke", NOT "pwke" — subsequence)
longestNonRepeatingSubstring(''); // → 0 (empty string)
longestNonRepeatingSubstring('z'); // → 1 (single char)
longestNonRepeatingSubstring('abcdef'); // → 6 (all unique → full length)
longestNonRepeatingSubstring('a b a b'); // → 3 (space counts as a character; e.g. "b a")
longestNonRepeatingSubstring('abc123abc');// → 6 ("abc123")
number. If you also want the substring itself, that's an extension — see Going further in the solution.s. "pwke" is a valid subsequence of "pwwkew" but not a substring; the correct substring answer is "wke" (length 3)."a b" has length 3 (a, space, b) and contains no duplicates.0, not undefined. A common bug is initialising max to undefined and short-circuiting on length 0.String.prototype mutators."🙂" made of surrogate pairs). The tests stick to BMP characters; if you want to handle astral plane characters, see Gotchas in the solution.