Skip to main content

Longest Substring Without Repeat

MediumPremium

Given a string s, write a function longestSubstringWithoutRepeat that returns the length of the longest substring without repeating characters. You may assume that the input string contains only ASCII characters. If the string is empty, return 0. The substring must consist of contiguous characters, and it cannot include any duplicate characters.

Examples

Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Input: s = "" Output: 0 Explanation: The string is empty, so the answer is 0.

Can you come up with a solution with a O(n) time complexity?