Lexicographic rank of a Binary String
Last Updated :
29 Jan, 2022
Given a binary string S of length N, the task is to find the lexicographic rank of the given string.
Examples:
Input: S = "001"
Output: 8
Explanation:
Strings in order of their increasing rank:
"0" = 1,
“1” = 2,
“00” = 3,
“01” = 4,
“10” = 5,
“11” = 6,
“000” = 7,
"001" = 8.
Input: S = "01"
Output: 4
Naive Approach: The simplest approach is to generate all possible binary strings (consisting of 0s and 1s) up to length N and store them in an array. Once done, sort the array of strings in lexicographic order and print the index of the given string in the array.
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: The idea is to generate a binary string consisting of 0s and 1s by replacing every occurrence of 'a' with 0 and 'b' with 1. Therefore, the strings in the list become:
"a" = 0,
“b” = 1,
“aa” = 00,
“ab” = 01,
“ba” = 10,
“bb” = 11,
“aaa” = 000,
"aab" = 001 and so on.
It can be observed that for a string of size N, there exist (2N - 2) strings of length less than N before that given string. Let it be equal to X. Now, its lexicographic position among strings of length N can be calculated by converting the string into its decimal equivalent number and adding 1 to it. Let it be equal to Y.
Rank of a string = X + Y + 1
= (2N - 2) + Y + 1
= 2N + Y - 1
Below is the implementation of the above approach:
C++
//C++ program for the above approach
#include <iostream>
using namespace std;
// Function to find the rank of a string
void findRank(string s)
{
// Store the length of the string
int N = s.size();
// Stores its equivalent decimal value
string bin;
// Traverse the string
for (int i = 0; i < N; i++)
{
if (s[i] == '0')
bin += "0";
else
bin += "1";
}
// Store the number of strings of
// length less than N occurring
// before the given string
long long X = 1ll<<N;
// Store the decimal equivalent
// number of string bin
long long res = 0, val = 1;
for(int i = N - 1; i >= 0; i--)
{
if (bin[i] == '1')
res += (val);
val *= 2ll;
}
long long Y = res;
// Store the rank in answer
long ans = X + Y - 1;
// Print the answer
cout << ans;
}
// Driver program
int main()
{
string S = "001";
findRank(S);
return 0;
}
// This code is contributed by jyoti369.
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
// Function to find the rank of a string
static void findRank(String s)
{
// Store the length of the string
int N = s.length();
// Stores its equivalent decimal value
StringBuilder sb = new StringBuilder("");
// Traverse the string
for (int i = 0; i < N; i++) {
if (s.charAt(i) == '0')
sb.append('0');
else
sb.append('1');
}
String bin = sb.toString();
// Store the number of strings of
// length less than N occurring
// before the given string
long X = (long)Math.pow(2, N);
// Store the decimal equivalent
// number of string bin
long Y = Long.parseLong(bin, 2);
// Store the rank in answer
long ans = X + Y - 1;
// Print the answer
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
String S = "001";
findRank(S);
}
}
Python3
# Python program for the above approach
# Function to find the rank of a string
def findRank(s):
# Store the length of the string
N = len(s);
# Stores its equivalent decimal value
sb = "";
# Traverse the string
for i in range(0,N):
if (s[i] == '0'):
sb += str('0');
else:
sb += str('1');
bin = str(sb);
# Store the number of strings of
# length less than N occurring
# before the given string
X = pow(2, N);
# Store the decimal equivalent
# number of string bin
Y = int(bin);
# Store the rank in answer
ans = X + Y - 1;
# Print the answer
print(ans);
# Driver Code
if __name__ == '__main__':
S = "001";
findRank(S);
# This code is contributed by shikhasingrajput
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to find the rank of a String
static void findRank(string s)
{
// Store the length of the String
int N = s.Length;
// Stores its equivalent decimal value
string bin = "";
// Traverse the String
for (int i = 0; i < N; i++)
{
if (s[i] == '0')
bin += "0";
else
bin += "1";
}
// Store the number of string s of
// length less than N occurring
// before the given String
int X = 1<<N;
// Store the decimal equivalent
// number of String bin
int res = 0, val = 1;
for(int i = N - 1; i >= 0; i--)
{
if (bin[i] == '1')
res += (val);
val *= 2;
}
int Y = res;
// Store the rank in answer
int ans = X + Y - 1;
// Print the answer
Console.Write(ans);
}
// Driver Code
public static void Main()
{
string S = "001";
findRank(S);
}
}
// This code is contributed by splevel62.
JavaScript
<script>
// Javascript program for the above approach
// Function to find the rank of a string
function findRank( s)
{
// Store the length of the string
var N = s.length;
// Stores its equivalent decimal value
var bin = "";
// Traverse the string
for (var i = 0; i < N; i++)
{
if (s[i] == '0')
bin += "0";
else
bin += "1";
}
// Store the number of strings of
// length less than N occurring
// before the given string
var X = 1<<N;
// Store the decimal equivalent
// number of string bin
var res = 0, val = 1;
for(var i = N - 1; i >= 0; i--)
{
if (bin[i] == '1')
res += (val);
val *= 2;
}
var Y = res;
// Store the rank in answer
var ans = X + Y - 1;
// Print the answer
document.write( ans);
}
// Driver program
var S = "001";
findRank(S);
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Lexicographic rank of a String Given a string str, find its rank among all its permutations when sorted lexicographically.Note: The characters in string are all unique.Examples:Input: str = "acb"Output: 2Explanation: If all the permutations of the string are arranged lexicographically they will be "abc", "acb", "bac", "bca", "cab
15+ min read
Lexicographic rank of a string using STL You are given a string, find its rank among all its permutations sorted lexicographically. Examples:Input : str[] = "acb"Output : Rank = 2Input : str[] = "string"Output : Rank = 598Input : str[] = "cba"Output : Rank = 6We have already discussed solutions to find Lexicographic rank of string In this
4 min read
Lexicographic rank of a string with duplicate characters Given a string s that may have duplicate characters. Find out the lexicographic rank of s. s may consist of lower as well as upper case letters. We consider the lexicographic order of characters as their order of ASCII value. Hence the lexicographical order of characters will be 'A', 'B', 'C', ...,
10 min read
Lexicographic rank of a string among all its substrings Given string str, the task is to find the rank of the given string among all its substrings arranged lexicographically. Examples: Input: S = "enren"Output: 7Explanation:All the possible substrings in the sorted order are {"e", "e", "en", "en", "enr", "enre", "enren", "n", "n", "nr", "nre", "nren", "
10 min read
Lexicographically Next Permutation of given String All the permutations of a word when arranged in a dictionary, the order of words so obtained is called lexicographical order. in Simple words, it is the one that has all its elements sorted in ascending order, and the largest has all its elements sorted in descending order. lexicographically is noth
8 min read
Make the String lexicographically larger Given a string str of length n. The task is to find a lexicographic largest string where you are allowed to shift one character from str to any other index only one time. Examples: Input: n = 3, str = "bac"Output: "cba"Explanation: We can perform the given operation exactly one time on the string st
5 min read