forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis_contains_unique_chars.py
31 lines (25 loc) · 877 Bytes
/
is_contains_unique_chars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
defis_contains_unique_chars(input_str: str) ->bool:
"""
Check if all characters in the string is unique or not.
>>> is_contains_unique_chars("I_love.py")
True
>>> is_contains_unique_chars("I don't love Python")
False
Time complexity: O(n)
Space complexity: O(1) 19320 bytes as we are having 144697 characters in unicode
"""
# Each bit will represent each unicode character
# For example 65th bit representing 'A'
# https://stackoverflow.com/a/12811293
bitmap=0
forchininput_str:
ch_unicode=ord(ch)
ch_bit_index_on=pow(2, ch_unicode)
# If we already turned on bit for current character's unicode
ifbitmap>>ch_unicode&1==1:
returnFalse
bitmap|=ch_bit_index_on
returnTrue
if__name__=="__main__":
importdoctest
doctest.testmod()