Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.

Examples:

s = “leetcode” return 0. s = “loveleetcode”, return 2.

Note: You may assume the string contain only lowercase letters.

Solution

Create an Array of length 26 and maintain frequency of the each character. Here ‘a’ having index as 0 and ‘z’ having index as 25. Iterate over the String and increament the value in the array.

After we build frequency Array, Just iterate a String one more time and check for each character, if frequency is 1 then that index is the answer.

Code

Output

0

We encourage you to write a comment if you have a better solution or having any doubt on the above topic.

Leave a Reply

Your email address will not be published. Required fields are marked *