Skip to main content
All Questions

Find the longest substring without repeating characters.

Medium

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.

Related courses

Course

Machine Learning Engineer Interview Prep

Land your dream machine learning role at Meta, Google, Amazon, Apple, Microsoft, Nvidia, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Explore ML system design, core concepts, coding, behavioral interviews, and more.

Course

Data Science Interview Prep

Land your dream data science role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Practice statistics, experimentation, coding, SQL, machine learning, behavioral interviews, and more.