17. First non-repeating character in a stream
The problem can be found at the following link: Question Link
Approach
To find the first non-repeating character in a stream, follow these steps:
Create a counter array,
cnt
, of size 26 to count the occurrences of characters in the input string. Initialize it with zeros.Initialize an empty vector called
last
to store the order of characters based on their first occurrence.Iterate through each character in the input string
A
.Check if the current character is the first occurrence. If it is, add it to the
last
vector.Find the first character in the
last
vector that has occurred only once.If there are no characters with a single occurrence in the
last
vector, add '#' to the output string.Otherwise, add the first occurrence element from the
last
vector to the output string.
Time and Auxiliary Space Complexity
Time Complexity:
O(n)
, where n is the length of the input string. We iterate through the string once.Auxiliary Space Complexity:
O(26)
, as the space used forcnt
andlast
is constant, having a size of26
.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.
Last updated