Skip to content

Commit 4cb0741

Browse files
abranheAndrade Junior
and
Andrade Junior
authored
Add is unique algorithm (abranhe#31)
Add is unique algorithm Co-authored-by: Andrade Junior <andradej@anchorloans.com> Co-authored-by: Abraham Hernandez <abraham@abranhe.com>
2 parents c1c0c94 + c7e9fda commit 4cb0741

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

allalgorithms/string/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from .palindrome_check import *
2+
from .is_unique import *
23
from .hamming_dist import *

allalgorithms/string/is_unique.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Check if a string has all unique characters.
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: José E. Andrade Jr.
7+
# Github: @andradejunior
8+
#
9+
10+
def is_unique(string_to_check):
11+
character_set = set()
12+
for character in string_to_check:
13+
if character in character_set:
14+
return False
15+
character_set.add(character)
16+
return True

docs/string/is-unique.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Is Unique
2+
3+
This algorithms checks if a string has all unique characters in O(n) time.
4+
5+
## Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
## Usage
12+
13+
```py
14+
from allalgorithms.string import is_unique
15+
16+
str = "abcdefg"
17+
18+
print(is_unique(str)
19+
# -> True
20+
21+
str = "test"
22+
23+
print(is_unique(str)
24+
# -> False
25+
```
26+
27+
## API
28+
29+
### is_unique(string)
30+
31+
> Return True if string has all unique characters, False otherwise
32+
33+
##### Params:
34+
35+
- `string`: Input String
36+

tests/test_string.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from allalgorithms.string import palindrome_check
3+
from allalgorithms.string import palindrome_check, is_unique
44

55

66
class TestSorting(unittest.TestCase):
@@ -13,5 +13,14 @@ def test_palindrome_check(self):
1313
self.assertEqual(True, palindrome_check("Was it a car or a cat I saw?"))
1414
self.assertEqual(False, palindrome_check("How are you?"))
1515

16+
def test_is_unique(self):
17+
self.assertEqual(True, is_unique("abcdefg"))
18+
self.assertEqual(True, is_unique("1234567"))
19+
self.assertEqual(True, is_unique("algorithms"))
20+
self.assertEqual(False, is_unique("abcdefa"))
21+
self.assertEqual(False, is_unique("abddefg"))
22+
self.assertEqual(False, is_unique("12345567"))
23+
24+
1625
if __name__ == "__main__":
1726
unittest.main()

0 commit comments

Comments
 (0)