I've written what I think is a constant time implementation of a uniqueness check. I've seen many algorithms online, which run in linear time, but was wondering whether my conclusion that this runs in constant time was correct.
I've seen many linear versions of this algorithm, but I haven't seen any that run in O(1).
Any feedback on style, flaws in the code, or how to improve the algorithm would be greatly appreciated.
from string import printable def is_unique(string): """Checks whether a string is unique or not. Big O: O(S * C) - Constant Time Where: `S` is the characters of the string being checked for uniqueness `C` is a set of all ASCII characters (not including the extended set) """ if len(string) == 1: # A single character is always going to be unique return True if len(string) > len(printable): # If there are more characters than the ASCII character set, # then we know there are duplicates return False found_character = bytearray(len(printable)) for character in string: for index, ascii_value in enumerate(printable): if character == ascii_value: if found_character[index] == True: return False # Set the index of the ASCII value in our bytearray to True found_character[index] = 1 break return True is_unique(string='abc')