Find first non repeating character in a String

if input = "total" answer = "o"
if input = "tweetweer" answer = "r"
if input = " gnu " answer = ...

Approach - to do in O(n)

1. Parse the string character by character and populate a map with count of each character
2. parse the string again and lookup character in map and get the count, store the key returning 1 first as a potential answer
3. As a part of step2 also check if any character has a count > 1 in which case return the answer stored in step2 else return null


Popular posts from this blog