forked from ruppysuppy/Daily-Coding-Problem-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path159.py
More file actions
34 lines (23 loc) · 667 Bytes
/
159.py
File metadata and controls
34 lines (23 loc) · 667 Bytes
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
"""
Problem:
Given a string, return the first recurring character in it, or null if there is no
recurring chracter.
For example, given the string "acbbac", return "b". Given the string "abcdef", return
null.
"""
from typing import Optional
def get_first_recurring_character(string: str) -> Optional[str]:
seen_characters = set()
for char in string:
if char in seen_characters:
return char
seen_characters.add(char)
return None
if __name__ == "__main__":
print(get_first_recurring_character("acbbac"))
print(get_first_recurring_character("abcdef"))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""