Longest Unique Subarray

Easy
0/40
profile
Contributed by
0 upvote

Problem statement

You are a developer at a text processing company, and you've been tasked with a feature that identifies the longest segment of a document that contains no repeated characters. This is useful for data validation and linguistic analysis.


A "subarray" or "substring" is a contiguous sequence of characters. For example, in the string "abac", "aba" is a substring, but "aac" is not.


Given an input string s, your task is to find the length of the longest substring that contains only distinct (unique) characters.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The input consists of a single line containing the string s.


Output Format:
The output should be a single integer representing the length of the longest substring with all distinct characters.


Note:
If the input string is empty, the longest such substring has a length of 0.

If all characters in the string are the same, the longest such substring has a length of 1.
Sample Input 1:
abacdeab


Sample Output 1:
5


Explanation for Sample 1:
The input string is "abacdeab". We can examine all substrings with unique characters:
 - "ab" (length 2)
 - "bac" (length 3)
 - "acde" (length 4)
 - "bacde" (length 5)
 - "cdeab" (length 5)
 The maximum length among these is 5.


Sample Input 2:
aaaaa


Sample Output 2:
1


Explanation for Sample 2:
The only substring with distinct characters is any single character "a". The length is 1.
Expected Time Complexity:
The expected time complexity is O(N), where N is the length of the string.


Constraints:
0 <= n <= 10^5, where n is the length of s.
s consists of lowercase English letters.

Time limit: 1 sec
Approaches (1)
Sliding Window with Hash Set
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Longest Unique Subarray
Full screen
Console