Find Mth lexicographically smallest Binary String with no two adjacent 1
Last Updated :
09 May, 2022
Given two integers N and M, the task is to find the Mth lexicographically smallest binary string (have only characters 1 and 0) of length N where there cannot be two consecutive 1s.
Examples:
Input: N = 2, M = 3.
Output: 10
Explanation: The only strings that can be made of size 2 are [“00”, “01”, “10”] and the 3rd string is “10”.
Input: N = 3, M = 2.
Output: 001
Approach: The problem can be solved based on the following approach:
Form all the N sized strings and find the Mth smallest among them.
Follow the steps mentioned below to implement the idea.
- For each character, there are two choices:
- Make the character 0.
- If the last character of the string formed till now is not 1, then the current character can also be 1.
- To implement this use recursion.
- As the target is to find the Mth smallest, for any character call the recursive function for 0 first and for 1 after that (if 1 can be used).
- Each time a string of length N is formed increase the count of strings
- If count = M, that string is the required lexicographically Mth smallest string.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string ans = "" ;
int Count = 0;
void findString( int idx, int n,
int m, string curr)
{
if (idx == n) {
if (Count == m - 1) {
ans = curr;
}
else {
Count += 1;
}
return ;
}
curr += "0" ;
findString(idx + 1, n, m, curr);
curr.pop_back();
if (curr[curr.length() - 1] != '1' ) {
curr += "1" ;
findString(idx + 1, n, m, curr);
curr.pop_back();
}
}
int main()
{
int N = 2, M = 3;
findString(0, N, M, "" );
cout << ans << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static String ans = "" ;
public static int Count = 0 ;
public static void findString( int idx, int n,
int m, String curr)
{
if (idx == n) {
if (Count == m - 1 ) {
ans = curr;
}
else {
Count += 1 ;
}
return ;
}
curr += "0" ;
findString(idx + 1 , n, m, curr);
curr=curr.substring( 0 ,curr.length()- 1 );
if (curr.length()== 0 || curr.charAt(curr.length() - 1 ) != '1' ) {
curr += "1" ;
findString(idx + 1 , n, m, curr);
curr=curr.substring( 0 ,curr.length()- 1 );
}
}
public static void main(String[] args)
{
int N = 2 , M = 3 ;
findString( 0 , N, M, "" );
System.out.println(ans);
}
}
|
Python3
ans = ""
Count = 0
def findString(idx, n, m, curr):
global ans,Count
if (idx = = n):
if (Count = = m - 1 ):
ans = curr
else :
Count + = 1
return
curr + = "0"
findString(idx + 1 , n, m, curr)
curr = curr[ 0 : len (curr) - 1 ]
if ( len (curr) = = 0 or curr[ len (curr) - 1 ] ! = '1' ):
curr + = "1"
findString(idx + 1 , n, m, curr)
curr = curr[ 0 : len (curr) - 1 ]
N,M = 2 , 3
findString( 0 , N, M, "")
print (ans)
|
Javascript
<script>
let ans = ""
let Count = 0
function findString(idx, n, m, curr){
if (idx == n){
if (Count == m - 1)
ans = curr
else
Count += 1
return
}
curr += "0"
findString(idx + 1, n, m, curr)
curr = curr.substring(0,curr.length - 1)
if (curr.length == 0 || curr[curr.length - 1] != '1' ){
curr += "1"
findString(idx + 1, n, m, curr)
curr = curr.substring(0,curr.length - 1)
}
}
let N = 2,M = 3
findString(0, N, M, "" )
document.write(ans)
</script>
|
C#
using System;
using System.Collections.Generic;
class GFG {
static String ans = "" ;
public static int Count = 0;
public static void findString( int idx, int n,
int m, string curr)
{
if (idx == n) {
if (Count == m - 1) {
ans = curr;
}
else {
Count += 1;
}
return ;
}
curr += "0" ;
findString(idx + 1, n, m, curr);
curr=curr.Substring(0,curr.Length-1);
if (curr.Length==0|| curr[(curr.Length - 1)] != '1' ) {
curr += "1" ;
findString(idx + 1, n, m, curr);
curr=curr.Substring(0,curr.Length-1);
}
}
public static void Main()
{
int N = 2, M = 3;
findString(0, N, M, "" );
Console.WriteLine(ans);
}
}
|
Time Complexity: O(2N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...