Unique Substring: Find the Longest Substring with All Distinct Characters
Question Details
Problem
Given a string s, find the longest substring that contains no repeated characters.
Return both the length and the substring itself. If there are multiple substrings of the same maximum length,
return the one that starts earliest.
python
def longest_unique_substring(s: str) -> tuple[int, str]:
pass
**Input**: s = "abcabcbb"
**Output**: (3, "abc")
**Input**: s = "bbbbb"
**Output**: (1, "b")
**Input**: s = "pwwkew"
**Output**: (3, "wke")
**Input**: s = ""
**Output**: (0, "")
Follow-ups
- Implement this using the sliding window technique. What is the time and space complexity?
- How does replacing the character set check with a hash map of last-seen positions improve the approach?
- Extend to find the longest substring with at most
kdistinct characters. - How would you find ALL substrings of the maximum length, not just the first one?
Full Details
Problem
Given a string s, find the longest substring that contains no repeated characters.
Return both the length and the substring itself. If there are multiple substrings of the same maximum length,
return the one that starts earliest.
python
def longest_unique_substring(s: str) -> tuple[int, str]:
pass
**Input**: s = "abcabcbb"
**Output**: (3, "abc")
**Input**: s = "bbbbb"
**Output**: (1, "b")
**Input**: s = "pwwkew"
**Output**: (3, "wke")
**Input**: s = ""
**Output**: (0, "")
Follow-ups
- Implement this using the sliding window technique. What is the time and space complexity?
- How does replacing the character set check with a hash map of last-seen positions improve the approach?
- Extend to find the longest substring with at most
kdistinct characters. - How would you find ALL substrings of the maximum length, not just the first one?
About This Question
This is a reported interview question from a moveworks interview during the phone round.
It covers the following topics: Strings, Sliding Window, Phone, Coding, Hash Table, Onsite .