MEDIUM NC#16 Blind #50 Sliding Window

3. Longest Substring Without Repeating Characters

📖 Problem

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

🧠 Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • Array methods (`push`, `pop`, `shift`, `slice`)
  • Object/Map/Set usage patterns
  • Function parameter and return typing
  • String/array indexing
  • Set/Map frequency counting
  • Pointer movement invariants

Logical Thinking Concepts

  • Define invariants before coding
  • Check edge cases first (`[]`, single element, duplicates)
  • Estimate time/space before implementation
  • Apply Sliding Window reasoning pattern
  • Apply Two Pointers reasoning pattern

💡 Approach

  • Use sliding window with left and right pointers
  • Use hash map to track last seen index of each character
  • When duplicate found, move left pointer past previous occurrence
  • Track maximum window size
  • Time: O(n), Space: O(min(m, n)) where m is charset size

🛠️ Hints & Pitfalls

Hints

  • Use sliding window with left and right pointers
  • Use hash map to track last seen index of each character
  • When duplicate found, move left pointer past previous occurrence

Common Pitfalls

  • Track maximum window size
  • Time: O(n), Space: O(min(m, n)) where m is charset size

🧪 Test Cases

Hidden tests on submit: 3

Test Case 1
Not run
Input:
lengthOfLongestSubstring('abcabcbb');
Expected:
3
Test Case 2
Not run
Input:
lengthOfLongestSubstring('bbbbb');
Expected:
1
Test Case 3
Not run
Input:
lengthOfLongestSubstring('pwwkew');
Expected:
3

📝 Code Editor

📚 Reference Solution

⌘K Search ⌘↩ Run ⌘S Submit