Saturday, May 10, 2008

First Non Repeating Character

Find the first non-repeating character in a string:("ABCA" -> B )

Solution:

Let the given input be of n characters.(let it be inputArray[])
First have an array of 256 characters which will store the repeat count of each character (let it be countArray[]).
Now scan through the input and update the increment count of that character in the above countArray[].
After the above exercise is done scan through the inputArray[]. For each character in the array check if the count in the countArray[] is 1. Break here and print the output as the current character in inputArray[].

2 comments:

Mad said...

Try the same algorithm with "ACBA"
As per algo this will return B. But the expected result is C

Ghotter said...

I dont think so...If u scan through the inputArr (and not the countArr) you should get 'C' as the output.