Common characters in n strings | Python3 Program to find all the common characters in n strings ; primary array for common characters we assume all characters are seen before . ; for each strings ; secondary array for common characters Initially marked false ; for every character of ith strings ; if character is present in all strings before , mark it . ; copy whole secondary array into primary ; displaying common characters ; Driver 's Code
MAX_CHAR = 26 NEW_LINE def commonCharacters ( strings , n ) : NEW_LINE INDENT prim = [ True ] * MAX_CHAR NEW_LINE for i in range ( n ) : NEW_LINE INDENT sec = [ False ] * MAX_CHAR NEW_LINE for j in range ( len ( strings [ i ] ) ) : NEW_LINE INDENT if ( prim [ ord ( strings [ i ] [ j ] ) - ord ( ' a ' ) ] ) : NEW_LINE INDENT sec [ ord ( strings [ i ] [ j ] ) - ord ( ' a ' ) ] = True NEW_LINE DEDENT DEDENT for i in range ( MAX_CHAR ) : NEW_LINE INDENT prim [ i ] = sec [ i ] NEW_LINE DEDENT DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( prim [ i ] ) : NEW_LINE INDENT print ( " % c ▁ " % ( i + ord ( ' a ' ) ) , end = " " ) NEW_LINE DEDENT DEDENT DEDENT strings = [ " geeksforgeeks " , " gemkstones " , " acknowledges " , " aguelikes " ] NEW_LINE n = len ( strings ) NEW_LINE commonCharacters ( strings , n ) NEW_LINE