Gamified challenge · Variable sliding window
beads is a string where each letter is the colour of one bead (r red, g green, and so on). You may cut out one contiguous stretch, but no colour may appear in it more than once.
Write longestUnique(beads) that returns the length of the longest such stretch. For "rgbrgbb" the answer is 3 (for example rgb); for "rrrr" it's 1, and an empty string gives 0.
Checking every stretch works for short strings; a window that grows on the right and shrinks on the left does it in one pass.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google