Given a binary string, S of size N, the task is to check if it is possible to partition the string into disjoint subsequences equal to “010”.
Examples:
Input: S = “010100”
Output: Yes
Explanation: Partitioning the string in the manner 010100 to generate two subsequences equal to “010”.
Input: S = “010000”
Output: No
Approach: The idea is based on the observation that a given binary string will not satisfy the required condition if any of the following conditions holds true:
- If, at any point, the prefix count of ‘1’s is greater than the prefix count of ‘0’s.
- If, at any point, the suffix count of ‘1’s is greater than the suffix count of ‘0’s.
- If the count of ‘0’s is not equal to twice the count of ‘1’s in the entire string.
Follow the steps below to solve the problem:
- Initialize a boolean variable, res as true to check if the string, S satisfies the given condition or not.
- Create two variables, count0 and count1 to store the frequency of 0s and 1s in the string, S.
- Traverse the string, S in the range [0, N – 1] using the variable i
- If S[i] is equal to 1, increment the value of count1 by 1.
- Otherwise, increment the value of count0 by 1.
- Check if the value of count1 > count0, then update res as false.
- Check if the value of count0 is not equal to 2 * count1, then update res as false.
- Reset the value of count0 and count1 to 0.
- Traverse the string S in the reverse direction and repeat steps 3.1 to 3.3.
- If the value of res is still true, print “Yes” as the result, otherwise print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPossible(string s)
{
int n = s.size();
int count_0 = 0, count_1 = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == '0' )
++count_0;
else
++count_1;
if (count_1 > count_0)
return false ;
}
if (count_0 != (2 * count_1))
return false ;
count_0 = 0, count_1 = 0;
for ( int i = n - 1; i >= 0; --i) {
if (s[i] == '0' )
++count_0;
else
++count_1;
if (count_1 > count_0)
return false ;
}
return true ;
}
int main()
{
string s = "010100" ;
if (isPossible(s))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
public class MyClass
{
static boolean isPossible(String s)
{
int n = s.length();
int count_0 = 0 , count_1 = 0 ;
for ( int i = 0 ; i < n; i++) {
if (s.charAt(i) == '0' )
++count_0;
else
++count_1;
if (count_1 > count_0)
return false ;
}
if (count_0 != ( 2 * count_1))
return false ;
count_0 = 0 ; count_1 = 0 ;
for ( int i = n - 1 ; i >= 0 ; --i) {
if (s.charAt(i) == '0' )
++count_0;
else
++count_1;
if (count_1 > count_0)
return false ;
}
return true ;
}
public static void main(String args[])
{
String s = "010100" ;
if (isPossible(s))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isPossible(s):
n = len (s)
count_0, count_1 = 0 , 0
for i in range (n):
if (s[i] = = '0' ):
count_0 + = 1
else :
count_1 + = 1
if (count_1 > count_0):
return False
if (count_0 ! = ( 2 * count_1)):
return False
count_0, count_1 = 0 , 0
for i in range (n - 1 , - 1 , - 1 ):
if (s[i] = = '0' ):
count_0 + = 1
else :
count_1 + = 1
if (count_1 > count_0):
return False
return True
if __name__ = = '__main__' :
s = "010100"
if (isPossible(s)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isPossible(String s)
{
int n = s.Length;
int count_0 = 0, count_1 = 0;
for ( int i = 0; i < n; i++)
{
if (s[i] == '0' )
++count_0;
else
++count_1;
if (count_1 > count_0)
return false ;
}
if (count_0 != (2 * count_1))
return false ;
count_0 = 0;
count_1 = 0;
for ( int i = n - 1; i >= 0; --i)
{
if (s[i] == '0' )
++count_0;
else
++count_1;
if (count_1 > count_0)
return false ;
}
return true ;
}
static public void Main()
{
String s = "010100" ;
if (isPossible(s))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isPossible(s)
{
let n = s.length;
let count_0 = 0, count_1 = 0;
for (let i = 0; i < n; i++) {
if (s[i] == '0' )
++count_0;
else
++count_1;
if (count_1 > count_0)
return false ;
}
if (count_0 != (2 * count_1))
return false ;
count_0 = 0; count_1 = 0;
for (let i = n - 1; i >= 0; --i) {
if (s[i] == '0' )
++count_0;
else
++count_1;
if (count_1 > count_0)
return false ;
}
return true ;
}
let s = "010100" ;
if (isPossible(s))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)