First and Last Longest Repeat-Free Substrings
Problem statement
A repeat-free stretch of a string is a contiguous substring in which no character appears more than once.
Given a string s, find the greatest length of a repeat-free stretch. Several stretches may share that length. Return a list of two strings: the one that starts earliest in s, then the one that starts latest. If only one stretch has the greatest length, return it twice.
Function
longestUniqueStretches(s: String) → String[]Examples
Example 1
s = "tacocat"return = ["taco","ocat"]Both "taco" (indexes 0 to 3) and "ocat" (indexes 3 to 6) have four different letters. No longer stretch works: each of the three 5-letter stretches holds two cs. So the first longest stretch is "taco" and the last is "ocat".
Example 2
s = "xyzzy"return = ["xyz","xyz"]The only 3-character stretch with no repeat is "xyz"; every longer stretch holds both zs. With one longest stretch, both answers are the same.
Example 3
s = "Aa1aA"return = ["Aa1","1aA"]Upper and lower case letters are different characters, so "Aa1" has no repeat. "a1a" repeats a, and "1aA" is the last 3-character stretch with no repeat. Both 4-character stretches repeat a.
Constraints
1 <= s.length <= 10^4scontains only English letters (a-z,A-Z) and digits (0-9); upper and lower case count as different characters