Longest Prefix With No Repeats
MediumPractice library · +20 XPstringssliding windowhash set
Given a string S, find the length of the longest prefix of S that contains no repeating characters. If the entire string has no repeating characters, return the length of the string.
Input
A single string S (consisting of lowercase English letters).
Output
A single integer representing the length of the longest prefix of S that contains no repeating characters.
Constraints
1 <= |S| <= 200000
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
abcabcbb
Output
3
The longest prefix with no repeating characters is 'abc', length 3.
Example 2
Input
abcdefg
Output
7
The entire string has no repeating characters, so the length is 7.
Hints
Similar problems
- String with Most Unique CharactersMedium
- Longest Substring Without RepeatsMedium
- Longest Substring With At Most K Distinct CharactersMedium
- Longest Alternating SubstringMedium
- Count Subarrays with Distinct ElementsMedium
- Longest Subarray With Sum XMedium
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
abcdefg