Check if a string can be emptied by removing all subsequences of the form “10”
Last Updated :
13 Apr, 2021
Given a binary string str, the task is to check if the string can be emptied by removing all subsequences of the form “10”
Examples:
Input: str = “11011000”
Output: Yes
Explanation:Following steps are required to be performed to empty the given string “11011000” ? “111000” ? “1100” ? “10” ? “”
Input: 101001
Output: No
Approach: Follow the steps below to solve the problem:
- Traverse the string and store all 1s in a Stack.
- If str[i] = 1, then store it in the stack.
- If str[i] = 0 and stack is not empty, then pop the character at the top of the stack.
- Otherwise, return false.
- If stack is empty, return true.
- Otherwise, return false
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isReducible(string str)
{
int N = str.size();
stack< char > s;
for ( int i = 0; i < N; i++) {
if (str[i] == '1' )
s.push(str[i]);
else if (!s.empty())
s.pop();
else
return false ;
}
return s.empty();
}
int main()
{
string str = "11011000" ;
if (isReducible(str))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static boolean isReducible(String str)
{
int N = str.length();
ArrayList<Character> s = new ArrayList<Character>();
for ( int i = 0 ; i < N; i++)
{
if (str.charAt(i) == '1' )
s.add(str.charAt(i));
else if (s.size() > 0 )
s.remove(s.size() - 1 );
else
return false ;
}
if (s.size() == 0 )
{
return true ;
}
else {
return false ;
}
}
public static void main (String[] args)
{
String str = "11011000" ;
if (isReducible(str))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
def isReducible( Str ) :
N = len ( Str )
s = []
for i in range (N) :
if ( Str [i] = = '1' ) :
s.append( Str [i])
elif ( len (s) > 0 ) :
del s[ len (s) - 1 ]
else :
return False
if ( len (s) = = 0 ) :
return True
else :
return False
Str = "11011000"
if (isReducible( Str )) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG {
static bool isReducible( string str)
{
int N = str.Length;
List< char > s = new List< char >();
for ( int i = 0; i < N; i++) {
if (str[i] == '1' )
s.Add(str[i]);
else if (s.Count > 0)
s.RemoveAt(s.Count - 1);
else
return false ;
}
if (s.Count == 0)
{
return true ;
}
else {
return false ;
}
}
static void Main()
{
string str = "11011000" ;
if (isReducible(str))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isReducible(str)
{
let N = str.length;
let s = [];
for (let i = 0; i < N; i++) {
if (str[i] == '1' )
s.push(str[i]);
else if (s.length > 0)
s.pop();
else
return false ;
}
if (s.length == 0)
{
return true ;
}
else {
return false ;
}
}
let str = "11011000" ;
if (isReducible(str))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...