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)
| 1 | def 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 | |
| 28 | if __name__ == "__main__": |
nothing calls this directly
no outgoing calls
no test coverage detected