s, return the length of the longest substring (contiguous block of characters) that contains every character at most once.Longest Distinct Substring
L4 Easy Sliding Window
Concept
A sliding window keeps a range whose contents satisfy some condition. Track which characters are inside the window, and shrink from the left whenever a repeat appears.
Given a string
Examples
▸ s = "abcabcbb"
→ 3
▸ s = "bbbbb"
→ 1
▸ s = "pwwkew"
→ 3
Progressive Hints
Hint 1 · Nudge
Grow the window to the right, and when a repeat shows up, shrink from the left until it's gone.
Hint 2 · Plan
Keep a set of the characters in the current window and a left pointer. For each position on the right: while the new character is already in the set, remove the leftmost character and advance the left pointer. Then add the new character and record the window size.
Hint 3 · Approach
left = 0, best = 0, and a set of seen characters. For each right, c: while c is in the set, drop s[left] and advance left; then add c and set best to the larger of best and right - left + 1. Return best.
All hints are out. Take a breath and give it a shot.
Output
// Run your code to see the output here.