Find the duplicate characters in a string in O ( 1 ) space | Function to find duplicate characters in str1ing without using any additional data str1ucture ; Check if ( i + ' a ' ) is present in str1 at least once or not . ; Check if ( i + ' a ' ) is present in str1 at least twice or not . ; Iterate over the characters of the str1ing str1 ; If str1 [ i ] has already occurred in str1 ; Set ( str1 [ i ] - ' a ' ) - th bit of second ; Set ( str1 [ i ] - ' a ' ) - th bit of second ; Iterate over the range [ 0 , 25 ] ; If i - th bit of both first and second is Set ; Driver Code
def findDuplicate ( str1 , N ) : NEW_LINE INDENT first = 0 NEW_LINE second = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( first & ( 1 << ( ord ( str1 [ i ] ) - 97 ) ) ) : NEW_LINE INDENT second = second | ( 1 << ( ord ( str1 [ i ] ) - 97 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT first = first | ( 1 << ( ord ( str1 [ i ] ) - 97 ) ) NEW_LINE DEDENT DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( ( first & ( 1 << i ) ) and ( second & ( 1 << i ) ) ) : NEW_LINE INDENT print ( chr ( i + 97 ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " geeksforgeeks " NEW_LINE N = len ( str1 ) NEW_LINE findDuplicate ( str1 , N ) NEW_LINE DEDENT