Skip to main content

387 - First Unique Character in a String

Details

KeyValue
Linkhttps://leetcode.com/problems/first-unique-character-in-a-string/
LanguagePython 3
Runtime170 ms, faster than 54.13% of Python3 online submissions for First Unique Character in a String
Memory Usage14.2 MB, less than 59.00% of Python3 online submissions for First Unique Character in a String
Datastructuresstr
AlgorithmsIteration
ComplexityTime: O(N) Memory: O(1)

Procedure

  1. ...

Code

Option 1

class Solution:
def firstUniqChar(self, s: str) -> int:
d = {}
for char in s: d[char] = d.get(char, 0) + 1

for i, char in enumerate(s):
if d[char] == 1: return i

return -1

Option 2: One-Liner

class Solution:
def firstUniqChar(self, s: str) -> int:
return min(([s.index(char) for char in set(s) if s.count(char) == 1] or [-1]))