diff --git a/allalgorithms/string/__init__.py b/allalgorithms/string/__init__.py index 0774298..dd8aa8f 100644 --- a/allalgorithms/string/__init__.py +++ b/allalgorithms/string/__init__.py @@ -1,2 +1,3 @@ from .palindrome_check import * +from .is_unique import * from .hamming_dist import * diff --git a/allalgorithms/string/is_unique.py b/allalgorithms/string/is_unique.py new file mode 100644 index 0000000..986a30c --- /dev/null +++ b/allalgorithms/string/is_unique.py @@ -0,0 +1,16 @@ +# -*- coding: UTF-8 -*- +# +# Check if a string has all unique characters. +# The All ▲lgorithms library for python +# +# Contributed by: José E. Andrade Jr. +# Github: @andradejunior +# + +def is_unique(string_to_check): + character_set = set() + for character in string_to_check: + if character in character_set: + return False + character_set.add(character) + return True diff --git a/docs/string/is-unique.md b/docs/string/is-unique.md new file mode 100644 index 0000000..7dc439f --- /dev/null +++ b/docs/string/is-unique.md @@ -0,0 +1,36 @@ +# Is Unique + +This algorithms checks if a string has all unique characters in O(n) time. + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.string import is_unique + +str = "abcdefg" + +print(is_unique(str) +# -> True + +str = "test" + +print(is_unique(str) +# -> False +``` + +## API + +### is_unique(string) + +> Return True if string has all unique characters, False otherwise + +##### Params: + +- `string`: Input String + diff --git a/tests/test_string.py b/tests/test_string.py index 998daf0..3cca91c 100644 --- a/tests/test_string.py +++ b/tests/test_string.py @@ -1,6 +1,6 @@ import unittest -from allalgorithms.string import palindrome_check +from allalgorithms.string import palindrome_check, is_unique class TestSorting(unittest.TestCase): @@ -13,5 +13,14 @@ def test_palindrome_check(self): self.assertEqual(True, palindrome_check("Was it a car or a cat I saw?")) self.assertEqual(False, palindrome_check("How are you?")) + def test_is_unique(self): + self.assertEqual(True, is_unique("abcdefg")) + self.assertEqual(True, is_unique("1234567")) + self.assertEqual(True, is_unique("algorithms")) + self.assertEqual(False, is_unique("abcdefa")) + self.assertEqual(False, is_unique("abddefg")) + self.assertEqual(False, is_unique("12345567")) + + if __name__ == "__main__": unittest.main()
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: