Skip to content

Commit 307290d

Browse files
committed
feat(strings): is unique string
1 parent 6e55c5d commit 307290d

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

pystrings/is_unique/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Is unique
2+
3+
Determine whether a string is unique or not. Assume that the input string will only contain alphabets or spaces.
4+
5+
![](./is_unique_example.png)
6+

pystrings/is_unique/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from collections import defaultdict
2+
from typing import DefaultDict
3+
4+
5+
def is_unique(input_str: str) -> bool:
6+
"""
7+
Checks if the input string contains unique characters.
8+
Args:
9+
input_str (str): the input string to evaluate
10+
Returns:
11+
bool: True if the input string contains unique characters, False otherwise.
12+
"""
13+
frequency_map: DefaultDict[str, int] = defaultdict(int)
14+
15+
for char in input_str:
16+
if char not in frequency_map:
17+
frequency_map[char] = 1
18+
else:
19+
return False
20+
21+
return True
37 KB
Loading
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import unittest
2+
from . import is_unique
3+
4+
5+
class IsUniqueTestCases(unittest.TestCase):
6+
7+
def test_1(self):
8+
"""should return true for abCDefGh"""
9+
input_string = "abCDefGh"
10+
actual = is_unique(input_string)
11+
self.assertTrue(actual)
12+
13+
def test_2(self):
14+
"""should return false for nonunique"""
15+
input_string = "nonunique"
16+
actual = is_unique(input_string)
17+
self.assertFalse(actual)
18+
19+
def test_3(self):
20+
"""should return true for abCedFghI"""
21+
input_string = "abCedFghI"
22+
actual = is_unique(input_string)
23+
self.assertTrue(actual)
24+
25+
def test_4(self):
26+
"""should return false for I Am Not Unique"""
27+
input_string = "I Am Not Unique"
28+
actual = is_unique(input_string)
29+
self.assertFalse(actual)
30+
31+
def test_5(self):
32+
"""should return False for heythere"""
33+
input_string = "heythere"
34+
actual = is_unique(input_string)
35+
self.assertFalse(actual)
36+
37+
def test_6(self):
38+
"""should return True for hi"""
39+
input_string = "hi"
40+
actual = is_unique(input_string)
41+
self.assertTrue(actual,)
42+
43+
44+
if __name__ == '__main__':
45+
unittest.main()

0 commit comments

Comments
 (0)