Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Sliding Window pattern, in the Variable Size (Condition-Based) subpattern.

Problem statement

Given a string s, find the length of the longest substring without duplicate characters.

Example 1:

Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

Constraints:

- 0 <= s.length <= 5 104

- s consists of English letters, digits, symbols and spaces.

Topics and companies

Topics: Hash Table, String, Sliding Window.

Reported in interviews at Amazon, Bloomberg, Microsoft, Facebook, Apple, Adobe, eBay, Goldman Sachs, Google, Alation, VMware, Oracle and others.

How to practise Longest Substring Without Repeating Characters on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.

Open Longest Substring Without Repeating Characters in the code editor, or read the Longest Substring Without Repeating Characters editorial for a worked solution with its approach and complexity analysis.

Where Longest Substring Without Repeating Characters sits in the DSA pattern sheet

Problems related to Longest Substring Without Repeating Characters

Other problems that use the same Variable Size (Condition-Based) and Sliding Window techniques:

Opening the problem

Progress is saved as you study.