-
Notifications
You must be signed in to change notification settings - Fork 2
/
387.py
52 lines (41 loc) · 1.35 KB
/
387.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
# [ LeetCode ] 387. First Unique Character in a String
def solution(s: str) -> int:
characters: dict[str, int] = {}
for idx, character in enumerate(s):
if character in characters:
characters[character] = -1
else:
characters[character] = idx
indexes: list[int] = sorted(characters.values())
for idx in indexes:
if idx != -1:
return idx
return -1
def another_solution(s: str) -> int:
characters: dict[str, int] = {}
seen: set = set()
for idx, character in enumerate(s):
if character in seen and character in characters:
characters.pop(character)
elif character not in seen:
seen.add(character)
characters[character] = idx
return next(iter(characters.values())) if characters else -1
if __name__ == "__main__":
cases: list[dict[str, dict[str, str]] | int] = [
{
"input": {"s": "leetcode"},
"output": 0
},
{
"input": {"s": "loveleetcode"},
"output": 2
},
{
"input": {"s": "aabb"},
"output": -1
}
]
for case in cases:
assert case["output"] == solution(s=case["input"]["s"])
assert case["output"] == another_solution(s=case["input"]["s"])