Longest Substring Without Repeating Characters
A simple explanation and clean Python solution for this Blind 75 problem.
π§© Longest Substring Without Repeating Characters
You are given a string.
Find the longest part (substring) that has no repeating characters.
π’ Example
Input:
"abcabcbb"| Substring | Has repeats? | Length |
|---|---|---|
| abc | β unique | 3 |
| abca | β repeats | - |
| bca | β unique | 3 |
β
Longest substring = "abc"
Answer = 3
π‘ Real-World Example
Imagine typing a password and checking the longest part without repeated letters,
since repeating ones make it weak.
Or in music, you might check the longest streak of non-repeating notes π΅.
βοΈ Approaches
π₯ Approach 1: Brute Force
Try all substrings and check if all characters are unique.
def longestSubstring(s):
longest = 0
for i in range(len(s)):
for j in range(i, len(s)):
part = s[i:j+1]
if len(set(part)) == len(part): # all unique
longest = max(longest, len(part))
return longestβ± Time: O(nΒ²) π¦ Space: O(n)
β Easy to understand β Slow for big strings
π₯ Approach 2: Sliding Window (Efficient β )
Use two pointers β left and right.
Expand right, shrink left when duplicates appear.
def longestSubstring(s):
seen = set()
left = 0
longest = 0
for right in range(len(s)):
while s[right] in seen:
seen.remove(s[left])
left += 1
seen.add(s[right])
longest = max(longest, right - left + 1)
return longestβ± Time: O(n) π¦ Space: O(n)
β Fast and simple for large input β Common interview pattern
π₯ Approach 3: Using Dictionary (Index Map)
Keep track of the last seen index for each character.
def longestSubstring(s):
seen = {}
left = 0
longest = 0
for right, char in enumerate(s):
if char in seen and seen[char] >= left:
left = seen[char] + 1
seen[char] = right
longest = max(longest, right - left + 1)
return longestβ± Time: O(n) π¦ Space: O(n)
β Cleanest version β Most preferred for interviews
π§ Real-Life Uses
| Use Case | Example |
|---|---|
| π Password Strength | Check longest non-repeating characters |
| π± Text Analysis | Find unique letter streaks in input |
| πΉ Game Logic | Longest unique sequence of moves |
| π¬ Chat Apps | Detect longest non-repeated pattern |
β οΈ Drawbacks
| Drawback | Explanation |
|---|---|
| π Brute Force is slow | Checks every substring |
| πΎ Uses memory | Set/dict store characters |
| π§© Works only on characters | Modify to support words |
π Example Dry Run
Input: "pwwkew"
| Step | Left | Right | Window | Longest |
|---|---|---|---|---|
| 0 | 0 | 0 | "p" | 1 |
| 1 | 0 | 1 | "pw" | 2 |
| 2 | 0 | 2 | "pww" β β duplicate | 2 |
| 3 | 1 | 3 | "wk" | 2 |
| 4 | 1 | 4 | "wke" | 3 β |
| 5 | 1 | 5 | "wkew" β β duplicate | 3 |
β Final Answer = 3 ("wke")
π§Ύ Summary Table
| Approach | Logic | Time | Space | Notes |
|---|---|---|---|---|
| Brute Force | Try all substrings | O(nΒ²) | O(n) | Simple but slow |
| Sliding Window (Set) | Move window & remove duplicates | O(n) | O(n) | Best balance |
| Index Map (Dict) | Track last seen index | O(n) | O(n) | Fastest β |
π‘ Key Takeaways
- Use sliding window for βlongest substringβ or βsubarrayβ problems.
- Use a set or dictionary to track duplicates.
- Practice dry runs to understand window movement.
β¨ Real-World Analogy
Imagine walking on tiles labeled with letters:
A - B - C - A - B - C - B - BYou keep walking until you step on a tile youβve already seen. Then, step back and start again. The farthest you go without repeating = your answer.
π Final Thought
This problem helps you understand:
- Sliding Window Technique πͺ
- HashSets & HashMaps πΎ
- Thinking in βexpand & shrinkβ windows
Once you learn this, you can easily move to:
- Longest Subarray with Sum K
- Longest Substring with at most K distinct characters
- Minimum Window Substring
---