MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / is_contains_unique_chars

Function is_contains_unique_chars

strings/is_contains_unique_chars.py:1–25  ·  view source on GitHub ↗

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

(input_str: str)

Source from the content-addressed store, hash-verified

1def is_contains_unique_chars(input_str: str) -> bool:
2 """
3 Check if all characters in the string is unique or not.
4 >>> is_contains_unique_chars("I_love.py")
5 True
6 >>> is_contains_unique_chars("I don't love Python")
7 False
8
9 Time complexity: O(n)
10 Space complexity: O(1) 19320 bytes as we are having 144697 characters in unicode
11 """
12
13 # Each bit will represent each unicode character
14 # For example 65th bit representing 'A'
15 # https://stackoverflow.com/a/12811293
16 bitmap = 0
17 for ch in input_str:
18 ch_unicode = ord(ch)
19 ch_bit_index_on = pow(2, ch_unicode)
20
21 # If we already turned on bit for current character's unicode
22 if bitmap >> ch_unicode & 1 == 1:
23 return False
24 bitmap |= ch_bit_index_on
25 return True
26
27
28if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected