First Non-Repeating Character

EASY

Implement a function to find the first non-repeating character in a string and return its index. If no such character exists, return -1.

Examples:

Example 1:

Input: s = "leetcode"
Output: 0 (character 'l')

Example 2:

Input: s = "loveleetcode"
Output: 2 (character 'v')

Example 3:

Input: s = "aabb"
Output: -1

Constraints:

  • 1 <= s.length <= 105
  • s contains only lowercase English letters.

Function Signature (Python):

class Solution:
    def firstUniqChar(self, s: str) -> int:
        # Your code here
        pass

 

Nerchuko Academy · Free DS Interview Prep