Loading...
Loading...
A radio app plays one song per letter of a code, and listeners like stretches with no song repeated. Read a code of small letters and find the longest block of consecutive letters in which no letter appears twice. Print its length, and the block itself. If several blocks have that length, print the one that starts first.
Example 1
Input
abcabcbb
Output
3 abc
abc, bca, cab all have length 3; abc starts first.
Example 2
Input
pwwkew
Output
3 wke
pwke is not a block of consecutive letters.
Try the problem first. Open one hint at a time.
Checking every block separately is O(n²) or worse. Instead, keep one window [start, i] with no repeats and slide it right.
Store in an int[26] the last index where each letter was seen (fill it with -1 first).
When letter ch was last seen at or after start, move start to just after that place. Then update the best length with > so the first block wins ties.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor