Lexicographic rank of a Binary String | Function to find the rank of a string ; Store the length of the string ; Stores its equivalent decimal value ; Traverse the string ; Store the number of strings of length less than N occurring before the given string ; Store the decimal equivalent number of string bin ; Store the rank in answer ; Prthe answer ; Driver Code
def findRank ( s ) : NEW_LINE INDENT N = len ( s ) ; NEW_LINE sb = " " ; NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT sb += str ( '0' ) ; NEW_LINE DEDENT else : NEW_LINE INDENT sb += str ( '1' ) ; NEW_LINE DEDENT DEDENT bin = str ( sb ) ; NEW_LINE X = pow ( 2 , N ) ; NEW_LINE Y = int ( bin ) ; NEW_LINE ans = X + Y - 1 ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "001" ; NEW_LINE findRank ( S ) ; NEW_LINE DEDENT