Binary string with given frequencies of sums of consecutive pairs of characters
Last Updated :
30 Dec, 2022
Given three integers P, Q, and R, the task is to generate a binary string with P, Q, and R pairs of consecutive characters with sum 0, 1, and 2 respectively.
Examples:
Input: P = 1, Q = 2, R = 2
Output: 111001
Explanation:
Substrings “00”, “10”, “01”, and “11” have sums 0, 1, 1, and 2 respectively.
Thus, the following set of substrings { {“11”}, {“11”}, {“10”}, {“00”}, {“01”} } satisfy the given constraints. Hence, the string formed by the substrings is 111001.
Input: P = 3, Q = 1, R = 0
Output: 10000
Approach: In order to solve this problem, we need to follow the following steps:
- If P and R are non-zero, then there is at least one pair of consecutive characters with sum 1. Thus, if Q provided is 0, in such a case, then no such string can be formed. Hence, return -1.
- If Q is zero, and only one of P and R is non-zero, then append 0 P+1 times if P is non-zero or append 1 R+1 time if R is non-zero.
- If all of them are non-zero:
- Append 0 and 1 P + 1 and Q + 1 times respectively.
- Append 0 and 1 alternatingly Q – 1 time.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string build_binary_str( int p,
int q, int r)
{
string ans = "" ;
if (q == 0) {
if (p != 0 && r != 0) {
return "-1" ;
}
else {
if (p) {
ans = string(p + 1, '0' );
}
else {
ans = string(r + 1, '1' );
}
}
}
else {
for ( int i = 1; i <= q + 1; i++) {
if (i % 2 == 0) {
ans += '0' ;
}
else {
ans += '1' ;
}
}
ans.insert(1, string(p, '0' ));
ans.insert(0, string(r, '1' ));
}
return ans;
}
int main()
{
int p = 1, q = 2, r = 2;
cout << build_binary_str(p, q, r);
return 0;
}
|
Java
class GFG{
static String build_binary_str( int p,
int q, int r)
{
String ans = "" ;
if (q == 0 )
{
if (p != 0 && r != 0 )
{
return "-1" ;
}
else
{
if (p > 0 )
{
ans = Strings(p + 1 , '0' );
}
else
{
ans = Strings(r + 1 , '1' );
}
}
}
else
{
for ( int i = 1 ; i <= q + 1 ; i++)
{
if (i % 2 == 0 )
{
ans += '0' ;
}
else
{
ans += '1' ;
}
}
ans = ans.substring( 0 , 1 ) + Strings(p, '0' ) + ans.substring( 1 );
ans = Strings(r, '1' ) + ans;
}
return ans;
}
static String Strings( int p, char c)
{
String ans = "" ;
for ( int i = 0 ; i < p; i++)
ans += c;
return ans;
}
public static void main(String[] args)
{
int p = 1 , q = 2 , r = 2 ;
System.out.print(build_binary_str(p, q, r));
}
}
|
Python3
def build_binary_str(p, q, r):
ans = ""
if (q = = 0 ):
if (p ! = 0 and r ! = 0 ):
return "-1"
else :
if (p):
temp = ""
for i in range (p + 1 ):
temp + = '0'
ans = temp
else :
temp = ""
for i in range (r + 1 ):
temp + = '1'
ans = temp
else :
for i in range ( 1 , q + 2 ):
if (i % 2 = = 0 ):
ans + = '0'
else :
ans + = '1'
temp = ""
for i in range (p):
temp + = '0'
st = ""
st + = ans[ 0 ]
st + = temp
for i in range ( 1 , len (ans)):
st + = ans[i]
ans = st
temp = ""
for i in range (r):
temp + = '1'
ans = temp + ans
return ans
if __name__ = = '__main__' :
p = 1
q = 2
r = 2
print (build_binary_str(p, q, r))
|
C#
using System;
class GFG{
static String build_binary_str( int p,
int q, int r)
{
String ans = "" ;
if (q == 0)
{
if (p != 0 && r != 0)
{
return "-1" ;
}
else
{
if (p > 0)
{
ans = Strings(p + 1, '0' );
}
else
{
ans = Strings(r + 1, '1' );
}
}
}
else
{
for ( int i = 1; i <= q + 1; i++)
{
if (i % 2 == 0)
{
ans += '0' ;
}
else
{
ans += '1' ;
}
}
ans = ans.Substring(0, 1) +
Strings(p, '0' ) +
ans.Substring(1);
ans = Strings(r, '1' ) + ans;
}
return ans;
}
static String Strings( int p, char c)
{
String ans = "" ;
for ( int i = 0; i < p; i++)
ans += c;
return ans;
}
public static void Main(String[] args)
{
int p = 1, q = 2, r = 2;
Console.Write(build_binary_str(p,
q, r));
}
}
|
Javascript
<script>
function build_binary_str(p, q, r)
{
let ans = "" ;
if (q == 0) {
if (p != 0 && r != 0) {
return "-1" ;
}
else {
if (p) {
ans = new Array(p + 1, '0' );
}
else {
ans = String(r + 1, '1' );
}
}
}
else {
for (let i = 1; i <= q + 1; i++) {
if (i % 2 == 0) {
ans += '0' ;
}
else {
ans += '1' ;
}
}
ans = ans.substr(0, 1) + Strings(p, '0' ) + ans.substr(1);
ans = Strings(r, '1' ) + ans;
}
return ans;
}
function Strings(p, c)
{
let ans = "" ;
for (let i = 0; i < p; i++)
ans += c;
return ans;
}
let p = 1, q = 2, r = 2;
document.write(build_binary_str(p, q, r));
</script>
|
Time Complexity: O(P + Q + R), where P, Q, and R are the given integers.
Auxiliary Space: O(P + Q + R)
Share your thoughts in the comments
Please Login to comment...