Determine if a string has all Unique Characters | Python3 program to illustrate String with unique characters without using any data structure ; Assuming string can have characters a - z this has 32 bits set to 0 ; If that bit is already set in checker , return False ; Otherwise update and continue by setting that bit in the checker ; No duplicates encountered , return True ; Driver Code
import math NEW_LINE def uniqueCharacters ( str ) : NEW_LINE INDENT checker = 0 NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT bitAtIndex = ord ( str [ i ] ) - ord ( ' a ' ) NEW_LINE if ( ( bitAtIndex ) > 0 ) : NEW_LINE INDENT if ( ( checker & ( ( 1 << bitAtIndex ) ) ) > 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT checker = checker | ( 1 << bitAtIndex ) NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT input = " geekforgeeks " NEW_LINE if ( uniqueCharacters ( input ) ) : NEW_LINE INDENT print ( " The ▁ String ▁ " + input + " ▁ has ▁ all ▁ unique ▁ characters " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ String ▁ " + input + " ▁ has ▁ duplicate ▁ characters " ) NEW_LINE DEDENT DEDENT