Bitwise OR of N binary strings | Function to return the bitwise OR of all the binary strings ; Get max size and reverse each string Since we have to perform OR operation on bits from right to left Reversing the string will make it easier to perform operation from left to right ; Add 0 s to the end of strings if needed ; Perform OR operation on each bit ; Reverse the resultant string to get the final string ; Return the final string ; Driver code
def strBitwiseOR ( arr , n ) : NEW_LINE INDENT res = " " NEW_LINE max_size = - ( 2 ** 32 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT max_size = max ( max_size , len ( arr [ i ] ) ) NEW_LINE arr [ i ] = arr [ i ] [ : : - 1 ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT s = " " NEW_LINE for j in range ( max_size - len ( arr [ i ] ) ) : NEW_LINE INDENT s += '0' NEW_LINE DEDENT arr [ i ] = arr [ i ] + s NEW_LINE DEDENT for i in range ( max_size ) : NEW_LINE INDENT curr_bit = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT curr_bit = curr_bit | ord ( arr [ j ] [ i ] ) NEW_LINE DEDENT res += chr ( curr_bit ) NEW_LINE DEDENT res = res [ : : - 1 ] NEW_LINE return res NEW_LINE DEDENT arr = [ "10" , "11" , "1000001" ] NEW_LINE n = len ( arr ) NEW_LINE print ( strBitwiseOR ( arr , n ) ) NEW_LINE