#K66947. Longest Distinct Substring
Longest Distinct Substring
Longest Distinct Substring
You are given a string s
. Your task is to find the longest substring of s
that contains only distinct characters. In other words, no character occurs more than once in that substring.
This problem can be efficiently solved using the sliding window technique. The idea is to use two pointers to represent a window in the string, and to slide the window while keeping track of the last seen positions of characters. The time complexity of an optimal solution is \(O(n)\), where \(n\) is the length of the string.
For example:
- For
s = "abcabcbb"
, the answer is"abc"
. - For
s = "bbbbb"
, the answer is"b"
. - For
s = "pwwkew"
, the answer is"wke"
.
inputFormat
The input consists of a single line containing the string s
. The string can be empty or non-empty and may contain any printable characters.
outputFormat
Output a single line which is the longest substring of s
that contains only distinct characters. If there are multiple answers, output the one which appears first.
abcabcbb
abc
</p>