Skip to content

Commit 6d3a341

Browse files
authored
Merge pull request #168 from BrianLusina/feat/algorithms-hash-table-unique-char
feat(algorithms, hash table): first unique character in string
2 parents 1acda91 + 0eb558c commit 6d3a341

4 files changed

Lines changed: 73 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@
166166
* Two City Scheduling
167167
* [Test Two City Scheduling](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/greedy/two_city_scheduling/test_two_city_scheduling.py)
168168
* Hash Table
169+
* First Unique Character
170+
* [Test First Unique Character](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/hash_table/first_unique_character/test_first_unique_character.py)
169171
* Ransom Note
170172
* [Test Ransom Note](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/hash_table/ransom_note/test_ransom_note.py)
171173
* Heap
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# First Unique Character
2+
3+
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
4+
5+
## Examples
6+
7+
Example 1:
8+
9+
```text
10+
Input: s = "leetcode"
11+
Output: 0
12+
Explanation:
13+
The character 'l' at index 0 is the first character that does not occur at any other index.
14+
```
15+
16+
Example 2:
17+
18+
```text
19+
Input: s = "loveleetcode"
20+
Output: 2
21+
```
22+
23+
Example 3:
24+
25+
```text
26+
Input: s = "aabb"
27+
Output: -1
28+
```
29+
30+
## Constraints
31+
32+
- 1 <= `s.length` <= 10^5
33+
- `s` consists of lowercase English letters
34+
- There are no spaces in the string.
35+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from collections import Counter
2+
3+
4+
def first_unique_character(s: str) -> int:
5+
frequency = Counter(s)
6+
7+
for idx, letter in enumerate(s):
8+
if frequency[letter] == 1:
9+
return idx
10+
return -1
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import unittest
2+
from parameterized import parameterized
3+
from algorithms.hash_table.first_unique_character import first_unique_character
4+
5+
FIRST_UNIQUE_CHARACTER_TEST_CASES = [
6+
("leetcode", 0),
7+
("loveleetcode", 2),
8+
("aabb", -1),
9+
("baefeab", 3),
10+
("aabbcc", -1),
11+
("dajhfiuebdafsdhdgaj", 5),
12+
("xyurtwxwtryua", 12),
13+
("aeiouqwertyauieotweryqq", -1),
14+
("awsjuhfajwfnkag", 2),
15+
]
16+
17+
18+
class FirstUniqueCharacterTestCase(unittest.TestCase):
19+
@parameterized.expand(FIRST_UNIQUE_CHARACTER_TEST_CASES)
20+
def test_first_unique_character(self, s: str, expected: int):
21+
actual = first_unique_character(s)
22+
self.assertEqual(expected, actual)
23+
24+
25+
if __name__ == '__main__':
26+
unittest.main()

0 commit comments

Comments
 (0)