Generate N sized binary string with prefix S and is lexicographically smallest possible
Last Updated :
30 Jan, 2023
Given a binary string S, the task is to make a binary string of size N from the given string S (don’t change the position of characters) following the below conditions:
- The prefix of the string is S.
- If is the lexicographically smallest possible.
- Absolute difference between the number of 1s and 0s is minimum.
Examples:
Input: S = “101”, N = 8
Output: 10100011
Explanation: The prefix of output string is same as the string S.
The absolute difference between number of 0s and 1s is 0.
It is the lexicographically smallest possible which follows all the given condition.
Input: S = “001”, N = 7
Output: 0010011
Approach: This problem can be solved by using the Greedy Approach based on the following idea:
In the case of a binary string, a string starting with ‘0’ is lexicographically smaller than the one starting with ‘1‘.
So, firstly make S the prefix and then find how many more 0s and 1s can be added. To make it lexicographically smallest, add all the 0s first and then the 1s.
Follow the steps mentioned below to implement the approach.
- Count numbers of 1’s and 0’s and store them (say in count1 and count0 respectively).
- Find the difference between (say g) count0 and count1 and between N and length of S (say in a variable l).
- Calculate the size which we need to increment in string length to make string length = N and store it in l.
- Now add as many 0s (which will be (l-g)/2) such that the number of 0 becomes the same as N/2 and then add 1s for the remaining places.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
string find_string( int n, string s)
{
int count0 = 0, count1 = 0;
for ( int i = 0; i < s.length(); i++) {
if (s[i] == '0' ) {
count0++;
}
else {
count1++;
}
}
string sb = s;
int g = count0 - count1;
int l = n - s.length();
l -= g;
int u = l / 2;
while (u > 0) {
sb += '0' ;
u--;
}
if (l % 2 != 0) {
sb += '0' ;
}
while (sb.length() < n) {
sb += '1' ;
}
return sb;
}
int main()
{
int N = 7;
string S = "001" ;
cout << find_string(N, S);
}
|
Java
import java.io.*;
import java.lang.*;
class GFG {
String find_string( int n, String s)
{
int count0 = 0 , count1 = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
if (s.charAt(i) == '0' ) {
count0++;
}
else {
count1++;
}
}
String sb = s;
int g = count0 - count1;
int l = n - s.length();
l -= g;
int u = l / 2 ;
while (u > 0 ) {
sb += '0' ;
u--;
}
if (l % 2 != 0 ) {
sb += '0' ;
}
while (sb.length() < n) {
sb += '1' ;
}
return sb;
}
public static void main(String[] args)
{
int N = 7 ;
String S = "001" ;
GFG g = new GFG();
System.out.println(g.find_string(N, S));
}
}
|
Python3
def find_string(n, s):
count0 = 0
count1 = 0
for i in range ( len (s)):
if (s[i] = = '0' ):
count0 + = 1
else :
count1 + = 1
sb = s
g = count0 - count1
l = n - len (s)
l - = g
u = l / / 2
while (u > 0 ):
sb + = '0'
u - = 1
if (l % 2 ! = 0 ):
sb + = '0'
while ( len (sb) < n):
sb + = '1'
return sb
N = 7
S = "001"
print (find_string(N, S))
|
C#
using System;
class GFG {
string find_string( int n, string s)
{
int count0 = 0, count1 = 0;
for ( int i = 0; i < s.Length; i++) {
if (s[i] == '0' ) {
count0++;
}
else {
count1++;
}
}
string sb = s;
int g = count0 - count1;
int l = n - s.Length;
l -= g;
int u = l / 2;
while (u > 0) {
sb += '0' ;
u--;
}
if (l % 2 != 0) {
sb += '0' ;
}
while (sb.Length < n) {
sb += '1' ;
}
return sb;
}
public static void Main()
{
int N = 7;
string S = "001" ;
GFG g = new GFG();
Console.Write(g.find_string(N, S));
}
}
|
Javascript
<script>
function find_string(n, s)
{
let count0 = 0;
let count1 = 0;
for (let i = 0; i < s.length; i++) {
if (s[i] == '0' ) {
count0++;
}
else {
count1++;
}
}
let sb = s;
let g = count0 - count1;
let l = n - s.length;
l -= g;
let u = Math.floor(l / 2);
while (u > 0) {
sb += '0 ';
u--;
}
if (l % 2 != 0) {
sb += ' 0 ';
}
while (sb.length < n) {
sb += ' 1';
}
return sb;
}
let N = 7;
let S = "001" ;
document.write(find_string(N, S));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...