-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstUniqueCharacterInAString.py
59 lines (50 loc) · 1.48 KB
/
FirstUniqueCharacterInAString.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Source : https://leetcode.com/problems/first-unique-character-in-a-string
# Author : Hamza Mogni
# Date : 2022-01-20
#####################################################################################################
#
# Given a string s, find the first non-repeating character in it and return its index. If it does not
# exist, return -1.
#
# Example 1:
# Input: s = "leetcode"
# Output: 0
# Example 2:
# Input: s = "loveleetcode"
# Output: 2
# Example 3:
# Input: s = "aabb"
# Output: -1
#
# Constraints:
#
# 1 <= s.length <= 10^5
# s consists of only lowercase English letters.
#####################################################################################################
class Solution:
# Time: o(n)
# Space: o(n)
def firstUniqChar(self, s: str) -> int:
"""
We iterate over the string and count occurences
of each character.
then we reiterate over the string and return the
first chracter that appeared only once in the string
"""
char_count = dict()
for char in s:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
for idx, char in enumerate(s):
if char_count[char] == 1:
return idx
return -1
s = Solution()
test1 = s.firstUniqChar("leetcode")
test2 = s.firstUniqChar("loveleetcode")
test3 = s.firstUniqChar("aabb")
assert test1 == 0
assert test2 == 2
assert test3 == -1