Longest Substring With All Unique Characters
Implement lengthOfLongestUniqueSubstring
Given a string
s, return the length of the longest substring that contains no repeated characters.
Checking every starting index and re-scanning forward works, but it forgets everything the previous start already learned about the string. The sliding windowSliding WindowMaintaining a running result over a contiguous range that grows or shrinks one element at a time, instead of recomputing the result for every range from scratch. technique keeps a hash map of each character's most recent index: grow the window by moving the right edge forward, and whenever a character repeats inside the current window, jump the left edge straight past that character's earlier occurrence — no need to shrink one step at a time. Every character is visited once by the right pointer, so the whole scan runs in O(n).
Example 1:
Input: s = "abcba"
Output: 3
Example 2:
Input: s = "bbbbb"
Output: 1
Example 3:
Input: s = "pwwkew"
Output: 3
+ 9 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ s.length ≤ 5 × 10⁴ - ●
s consists of English letters, digits, symbols, and spaces
s =
abcba