Lexicographic rank of a Binary String | C # program to implement the above approach ; 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 string s 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 Code
using System ; class GFG { static void findRank ( string s ) { int N = s . Length ; string bin = " " ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '0' ) bin += "0" ; else bin += "1" ; } int X = 1 << N ; int res = 0 , val = 1 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( bin [ i ] == '1' ) res += ( val ) ; val *= 2 ; } int Y = res ; int ans = X + Y - 1 ; Console . Write ( ans ) ; } public static void Main ( ) { string S = "001" ; findRank ( S ) ; } }