String with Most Unique Characters
MediumPractice library · +20 XPstringssliding windowhash map
Given a string S, find the length of the longest substring of S that contains only unique characters. If there are multiple such substrings of the maximal length, return the one that appears earliest in S.
Input
The first line contains a string S.
Output
Print a single integer representing the length of the longest substring with unique characters.
Constraints
1 <= |S| <= 200000. S consists of lowercase English letters.
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
abcabcbb
Output
3
The longest substring with unique characters is 'abc', length 3.
Example 2
Input
bbbbb
Output
1
The longest substring with unique characters is 'b', length 1.
Hints
Similar problems
- Longest Substring With At Most K Distinct CharactersMedium
- Longest Substring Without RepeatsMedium
- Longest Prefix With No RepeatsMedium
- Longest Segment with At Most K Distinct ElementsMedium
- Longest Subarray with Equal HalvesMedium
- Distinct Elements in a Sliding WindowMedium
More topics: Strings problems, Hash Map and Hashing problems, Sliding Window problems
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
abcabcbb
Example 2 input
bbbbb