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 ; Print the answer ; Driver program
function findRank ( s ) { var N = s . length ; var bin = " " ; for ( var i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' ' ) bin += " " ; else bin += " " ; } var X = 1 << N ; var res = 0 , val = 1 ; for ( var i = N - 1 ; i >= 0 ; i -- ) { if ( bin [ i ] == ' ' ) res += ( val ) ; val *= 2 ; } var Y = res ; var ans = X + Y - 1 ; document . write ( ans ) ; } var S = " " ; findRank ( S ) ;