-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst_repeated_char.py
43 lines (35 loc) · 1.12 KB
/
first_repeated_char.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
#!/usr/bin/env python3
"""Print the first repeated character in a string."""
from collections import defaultdict
def first_repeated_char_v1(input_str: str):
"""Return the first repeated character in string."""
counter = defaultdict(int)
for char in input_str:
counter[char] += 1
if counter[char] == 2:
return char
return None
def first_repeated_char_v2(input_str: str):
counts = dict()
for char in input_str:
if char in counts:
return char
counts[char] = 1
return None
if __name__ == "__main__":
list(map(print,
(first_repeated_char_v1("ABBA"),
first_repeated_char_v1("ABCA"),
first_repeated_char_v1("DBCABA"),
first_repeated_char_v1("BCABA"),
first_repeated_char_v1("ABC"),)
)
)
list(map(print,
(first_repeated_char_v2("ABBA"),
first_repeated_char_v2("ABCA"),
first_repeated_char_v2("DBCABA"),
first_repeated_char_v2("BCABA"),
first_repeated_char_v2("ABC"),)
)
)