Back to Practice Dashboard
Top 150 InterviewEasy

Longest Substring Without Repeating Characters

Learn how to solve the 'Longest Substring Without Repeating Characters' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

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

Write a function lengthOfLongestSubstring(s: str) -> int.

Constraints
  • 0 <= len(s) <= 5 * 10^4
  • s consists of English letters, digits, symbols, and spaces

Examples

Example 1
Input
s = "abcabcbb"
Output
3
Explanation

The answer is "abc", with a length of 3.

Example 2
Input
s = "bbbbb"
Output
1
Explanation

The answer is "b", with a length of 1.

Example 3
Input
s = "pwwkew"
Output
3
Explanation

The answer is "wke", with a length of 3. Note that "pwke" is a subsequence, not a substring.

Need a Hint?
Maintain a sliding window boundary using two pointers. Expand the right boundary, and contract the left boundary if criteria are violated.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor