First Unique Character in a Stream
Problem statement
Characters arrive in the order of the string stream. After each arrival, append the earliest character seen so far whose frequency in the processed prefix is exactly one.
If the current prefix has no unique character, append #. Return the concatenation of all per-arrival answers. Comparisons are case-sensitive.
Sibling variants: same core, different skin
These four problems are sibling variants of the same first-unique pattern. Their story wrappers and query timing differ, while the core rule keeps the earliest value whose frequency is exactly one.
- Longest-Waiting Unique Task
- Dynamic Longest-Waiting Unique Task Queries
- First User to Log In Exactly Once
- First Unique Character in a Stream (current)
Function
firstUniqueAfterEach(stream: String) → StringExamples
Example 1
stream = "aabc"return = "a#bb"The first a is unique, then none is unique, and b remains first after c arrives.
Example 2
stream = "zz"return = "z#"The only character stops being unique after its second occurrence.
Example 3
stream = "abc"return = "aaa"The earliest a remains unique throughout all three prefixes.
Constraints
1 ≤ stream.length ≤ 100000.streamcontains only ASCII letters and digits.- The sentinel
#does not occur instream.