Longest fresh stretch
Inputs
text is 0 to 16 lowercase letters (a to f).
Your tasks
- Keep a
leftmarker at 0 andbestat 0. - Use a loop where
rightmoves alongtext. While the letter atrightalready appears betweenleftandright - 1, moveleftright by one. - Then update
bestwith the window lengthright - left + 1. - Complete
longestUniqueso it returnsbest. Return it, do not print it.
Example
With text = "abcabcbb", longestUnique returns 3.
Rules
- The stretch is neighbouring letters with no letter repeated.
Test data
TEST DATA 1: text is "abcabcbb"
TEST DATA 2: text is ""
Practice focus
Two pointers and sliding windows: loops that move two positions instead of one. Two pointers and sliding windows turn many quadratic answers into linear ones.