Skip to content

Commit 4846796

Browse files
author
Andrade Junior
committed
Add is unique algorithm
1 parent 1192952 commit 4846796

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

allalgorithms/string/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from .palindrome_check import *
2-
2+
from .is_unique 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy