Praveen-Kumar
Er... Will try... If PHP I guess I can easily say... There is a concept of associative arrays, where you can do this way: count["char"] = n and so on.
For the algorithm,
1. Define an empty associative array and initialize to 0. def count[] = 0;
2. For each character, check the occurrence and increment the particular array node. count["s"]++;
3. Loop through the array and get the first value, which is 1!
foreach (val as count)
if val is 1
print the index
The complexity is O(n).
I think that won't work.. If you are using associative arrays, you need to specify the count of the character as well as the index when it was first encountered.
For example, take the string "bbzaccddbcd"
If you fill an associative array with this string, with the key as the characters, you would get
array['b']=3, array['c']=3, array['a']=1, etc.. and array['z']=1
Now you will scan through the array and the first element that has the value 1 is 'a'. So, you will return a, while the answer is z.
That's why, you also need to store the index where each character is first encountered. Then compare that value also.. Just checking for the count being 1 is not enough.