Earliest Unrepeated Letter in a Stream
Problem statement
Letters arrive one at a time; stream lists them in arrival order. After each letter arrives, report the earliest-arrived letter that has so far appeared exactly once, or # if every letter seen so far has appeared at least twice.
Return a string of the same length as stream whose i-th character is the report after the first i + 1 letters. A letter that repeats never counts again, even if it appears a third time.
Function
firstUniqueSoFar(stream: String) → StringExamples
Example 1
stream = "abacbd"return = "aabbcc"After a: a. After ab: still a. The second a leaves b as the earliest single letter. Adding c keeps b. The second b leaves only c. Adding d keeps c. So the answer is "aabbcc".
Example 2
stream = "mmm"return = "m##"After the first m it is the answer. After two or three ms no letter has appeared exactly once, so both positions get #.
Example 3
stream = "qrsrq"return = "qqqqs"q stays the answer while r and s arrive and r repeats. When q repeats at the end, s is the only letter seen once.
Constraints
1 <= stream.length <= 5000streamcontains only lowercase English lettersatoz