File tree Expand file tree Collapse file tree 4 files changed +63
-2
lines changed Expand file tree Collapse file tree 4 files changed +63
-2
lines changed Original file line number Diff line number Diff line change 1
1
from .palindrome_check import *
2
-
2
+ from . is_unique import *
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change 1
1
import unittest
2
2
3
- from allalgorithms .string import palindrome_check
3
+ from allalgorithms .string import palindrome_check , is_unique
4
4
5
5
6
6
class TestSorting (unittest .TestCase ):
@@ -13,5 +13,14 @@ def test_palindrome_check(self):
13
13
self .assertEqual (True , palindrome_check ("Was it a car or a cat I saw?" ))
14
14
self .assertEqual (False , palindrome_check ("How are you?" ))
15
15
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
+
16
25
if __name__ == "__main__" :
17
26
unittest .main ()
You can’t perform that action at this time.
0 commit comments