Skip to content

Commit 3ed7904

Browse files
committed
AlgoExpert First Non-Repeating Character
1 parent c03d850 commit 3ed7904

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -987,15 +987,18 @@ First column links to the problem in AlgoExpert, second is the problem's difficu
987987

988988
[🔝 Back to Top 🔝](#coding-challenges)
989989

990-
| AlgoExpert Link | Difficulty | Solutions |
991-
| ----------------------------------------- | ------------ | ------------------------------------------------ |
992-
| [Generate Document][ae&generate-document] | 🟢 Easy | [![python](res/py.png)][ae&generate-document#py] |
993-
| [Insertion Sort][ae&insertion-sort] | 🟢 Easy | [![python](res/py.png)][ae&insertion-sort#py] |
994-
| [Merge Sort][ae&merge-sort] | 🟣 Very Hard | [![python](res/py.png)][ae&merge-sort#py] |
995-
| [Remove Islands][ae&remove-islands] | 🟠 Medium | [![python](res/py.png)][ae&remove-islands#py] |
990+
| AlgoExpert Link | Difficulty | Solutions |
991+
| ----------------------------------------------------------------- | ------------ | ------------------------------------------------------------ |
992+
| [First Non-Repeating Character][ae&first-non-repeating-character] | 🟢 Easy | [![python](res/py.png)][ae&first-non-repeating-character#py] |
993+
| [Generate Document][ae&generate-document] | 🟢 Easy | [![python](res/py.png)][ae&generate-document#py] |
994+
| [Insertion Sort][ae&insertion-sort] | 🟢 Easy | [![python](res/py.png)][ae&insertion-sort#py] |
995+
| [Merge Sort][ae&merge-sort] | 🟣 Very Hard | [![python](res/py.png)][ae&merge-sort#py] |
996+
| [Remove Islands][ae&remove-islands] | 🟠 Medium | [![python](res/py.png)][ae&remove-islands#py] |
996997

997998
[🔝 Back to Top 🔝](#coding-challenges)
998999

1000+
[ae&first-non-repeating-character]: https://www.algoexpert.io/questions/first-non-repeating-character
1001+
[ae&first-non-repeating-character#py]: algoexpert/first-non-repeating-character.py
9991002
[ae&generate-document]: https://www.algoexpert.io/questions/generate-document
10001003
[ae&generate-document#py]: algoexpert/generate-document.py
10011004
[ae&insertion-sort]: https://www.algoexpert.io/questions/merge-sort
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# First Non-Repeating Character
2+
# 🟢 Easy
3+
#
4+
# https://www.algoexpert.io/questions/first-non-repeating-character
5+
#
6+
# Tags: String - Hash Map
7+
8+
import timeit
9+
from collections import Counter
10+
11+
12+
class Solution:
13+
def firstNonRepeatingCharacter(self, string: str) -> int:
14+
# Write your code here.
15+
freq = Counter(string)
16+
for i, c in enumerate(string):
17+
if freq[c] == 1:
18+
return i
19+
# There are not non-repeating characters.
20+
return -1
21+
22+
23+
def test():
24+
executors = [Solution]
25+
tests = [
26+
["abcdcaf", 1],
27+
["faadabcbbebdf", 6],
28+
]
29+
for executor in executors:
30+
start = timeit.default_timer()
31+
for _ in range(1):
32+
for col, t in enumerate(tests):
33+
sol = executor()
34+
result = sol.firstNonRepeatingCharacter(t[0])
35+
exp = t[1]
36+
assert result == exp, (
37+
f"\033[93m» {result} <> {exp}\033[91m for"
38+
+ f" test {col} using \033[1m{executor.__name__}"
39+
)
40+
stop = timeit.default_timer()
41+
used = str(round(stop - start, 5))
42+
cols = "{0:20}{1:10}{2:10}"
43+
res = cols.format(executor.__name__, used, "seconds")
44+
print(f"\033[92m» {res}\033[0m")
45+
46+
47+
test()

0 commit comments

Comments
 (0)