Maintained by Vikas Dulgunde, software engineer
You are handed a string `s` and asked for a single number: the length of the longest stretch of adjacent characters that are all distinct. A stretch here means a contiguous slice of the string, so the characters you pick must sit next to each other rather than being scattered.
The moment a character would appear twice inside the slice you are tracking, that slice is no longer valid and cannot be counted. You want the longest valid slice anywhere in the string.
Characters can be any letter, digit, symbol, or space, and the string may be empty. For an empty string the answer is 0, since there is no slice at all. Note the difference from a subsequence problem: you may not skip over characters in the middle, the winning slice has to be one unbroken segment.
The slice "abc" has three distinct characters. Any attempt to extend further hits a repeat, so 3 is the best.
Every character is the same, so the longest slice without a repeat is a single "b".
"wke" is the longest valid slice. Note "pwke" is not a substring because it skips a character, and "pww" repeats w.
When the second d appears, the window slides past the first d, leaving "vdf" of length 3.
Visible test cases
A brute-force check of every possible slice works but is slow. Think about what changes when you grow a slice one character to the right.
Keep a sliding window with a left and right edge. When the character at the right edge already lives inside the window, you must move the left edge forward to drop the earlier copy.
Instead of moving the left edge one step at a time, store the last index at which you saw each character. That lets you jump the left edge directly past the previous occurrence in one move.
Check every starting position, extend as far as possible while characters stay unique, and track the longest length found.
Maintain a window of distinct characters using a left pointer, and store the most recent index of each character so the left pointer can jump instead of crawl.