Longest Substring Without Repeated Characters
Problem statement
Given s, return the length of its longest substring containing no repeated character.
A substring is a contiguous part of the string. Matching is case-sensitive, and spaces and punctuation count as characters. Return 0 for an empty string.
Function
longestUniqueSubstring(s: String) → intExamples
Example 1
s = "abcabcbb"return = 3abc is a substring with three different characters. Any substring of length four contains a repeated character, so the maximum length is 3.
Example 2
s = "abba"return = 2The substrings ab and ba both have length 2 with no repeated character. Both length-three substrings repeat a character, so the answer is 2.
Example 3
s = "a b a"return = 3A space counts as a character. The substring a b has three different characters. Every substring of length four contains two spaces, so the answer is 3.
Constraints
0 <= s.length <= 5000- Every character is printable ASCII, with a character code from
32through126.