Practice Problem
- Queue
- Hash Table
Maintain character frequencies and a queue of candidates, removing repeated characters from the front.
O(n)
O(1)
import java.util.*;
class FirstNonRepeatingStream {
private int[] count = new int[26];
private Queue<Character> queue = new LinkedList<>();
public char add(char c) {
count[c - 'a']++;
queue.offer(c);
while (!queue.isEmpty() && count[queue.peek() - 'a'] > 1) queue.poll();
return queue.isEmpty() ? '#' : queue.peek();
}
}