Skip to content

Add is unique algorithm #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions allalgorithms/string/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .palindrome_check import *
from .is_unique import *
from .hamming_dist import *
16 changes: 16 additions & 0 deletions allalgorithms/string/is_unique.py
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions docs/string/is-unique.md
Original file line number Diff line number Diff line change
@@ -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

11 changes: 10 additions & 1 deletion tests/test_string.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from allalgorithms.string import palindrome_check
from allalgorithms.string import palindrome_check, is_unique


class TestSorting(unittest.TestCase):
Expand All @@ -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()