Loading...
Loading...
Arjun is creating a username for a college fest app. The app gives him a long random code made of lowercase letters and digits. He may use any consecutive piece of the code, but the app does not allow a character to appear twice in a username.
Print the length of the longest piece of the code in which no character repeats.
Example 1
Input
rahul2005
Output
7
rahul20 has 7 different characters. Adding the next 0 would repeat 0.
Example 2
Input
aaaa
Output
1
Every piece longer than one character repeats a.
Try the problem first. Open one hint at a time.
Keep a piece from left to right that has no repeats, and move right one step at a time.
When the new character is already inside the piece, the piece must start just after that earlier copy.
Store the last index where each character was seen. An array of size 128 works for letters and digits.
Only move left forward. An old copy before left is not inside the piece, so ignore it.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor