Problem: Implement a function to find the first character in a string which only appears once.
For example: It returns ‘b’ when the input is “abaccdeff”.
Analysis: Our native solution for this problem may be scanning the input string from its beginning to end. We compare the current scanned character with every one behind it. If there is no duplication after it, it is a character appearing once. Since it compares each character with O(n) ones behind it, the overall time complexity is O(n2) if there are n characters in a string.
better way: a hashTable, key is the character, value is the occurrence. go through the list 2 times:
- if 1st time, add into the table, otherwise, add the value.
- find the 1st 1 occurrence character