LeetCode 387 - First Unique Character in a String Problem

Problem Statement

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


s = "leetcode"
return 0.

s = "loveleetcode",
return 2.

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


class Solution: def firstUniqChar(self, s: str) -> int: if not s: return -1 seen = {} for i in range(len(s)): if s[i] in seen: seen[s[i]] = -1 else: seen[s[i]] = i for key in seen.keys(): if seen[key]>=0: return seen[key] return -1