Check if a string can be made empty by repeatedly removing given subsequence
Last Updated :
03 Aug, 2021
Given a string str containing characters ‘G’ and ‘F’ only, the task is to check if the given string str can be made empty after removing all subsequences of the form “GFG”.
Examples:
Input : N = 6, str[] = “GFGFGG”
Output : Yes
Explanation : Two strings of “GFG” can be made with the first one with indices {0, 1, 5} and the second one with the remaining indices.
Input : N = 10, str = “GGFFGFGFFG”
Output : No
Explanation : It is not possible, because even after making all possible strings one F will remain.
Approach: It can be seen that in order to make GFG, 2 G and one F in the middle are needed, so if the count of the G is not equal to twice the count of F, it is never possible to make some number of GFG complete from the given string. Another thing to check is that the number of G in the latter thing must never be less than the number of F as the string is GGGFFGFGFFG, so it can be seen that the last two, FF have only one G, so it will not be possible to make GFG from both of them so our answer will again be No. And the last condition to check is that the count of G passed is never less than the count of F in order to make GFG. Follow the steps below to solve the problem:
- Initialize the variables countG and countF as 0 to store the count of G and F in the string str[].
- Iterate over the range [0, N] using the variable i and perform the following steps:
- If str[i] is equal to G, then, increase the value of countG by 1, else, increase the value of countF by 1.
- If 2*countF is not equal to countG, then, it’s not possible so print “NO” and return.
- Initialize the variables id as 0 to calculate the count of G and F till a particular index and variable flag as true to store the answer.
- Iterate over the range [0, N] using the variable i and perform the following steps:
- If str[i] is equal to ‘1’, then decrease the value of countG by 1 and increase the value of id by 1.
- Else, decrease the value of countF by 1 and decrease the value of id by 1.
- If id is less than 0, then extra G will be in the end and won’t be utilized so set the value of flag to false and break.
- If countG is less than countF, then extra F will be in the end and won’t be utilized so set the value of flag to false and break.
- If flag is equal to true, then, print “YES”, else print “NO”.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void findIfPossible( int N, string str)
{
int countG = 0, countF = 0;
for ( int i = 0; i < N; i++) {
if (str[i] == 'G' )
countG++;
else
countF++;
}
if (2 * countF != countG) {
cout << "NO\n" ;
}
else {
int id = 0;
bool flag = true ;
for ( int i = 0; i < N; i++) {
if (str[i] == 'G' ) {
countG--;
id++;
}
else {
countF--;
id--;
}
if (id < 0) {
flag = false ;
break ;
}
if (countG < countF) {
flag = false ;
break ;
}
}
if (flag) {
cout << "YES\n" ;
}
else {
cout << "NO\n" ;
}
}
}
int main()
{
int n = 6;
string str = "GFGFGG" ;
findIfPossible(n, str);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static void findIfPossible( int N, String str)
{
int countG = 0 , countF = 0 ;
for ( int i = 0 ; i < N; i++) {
if (str.charAt(i) == 'G' )
countG++;
else
countF++;
}
if ( 2 * countF != countG) {
System.out.println( "No" );
}
else {
int id = 0 ;
boolean flag = true ;
for ( int i = 0 ; i < N; i++) {
if (str.charAt(i) == 'G' ) {
countG--;
id++;
}
else {
countF--;
id--;
}
if (id < 0 ) {
flag = false ;
break ;
}
if (countG < countF) {
flag = false ;
break ;
}
}
if (flag) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
public static void main(String[] args)
{
int n = 6 ;
String str = "GFGFGG" ;
findIfPossible(n, str);
}
}
|
Python3
def findIfPossible(N, str_):
countG = 0
countF = 0
for i in range (N):
if str_[i] = = 'G' :
countG + = 1
else :
countF + = 1
if 2 * countF ! = countG:
print ( "NO" )
else :
id = 0
flag = True
for i in range (N):
if str_[i] = = 'G' :
countG - = 1
id + = 1
else :
countF - = 1
id - = 1
if id < 0 :
flag = False
break
if countG < countF:
flag = False
break
if flag:
print ( "YES" )
else :
print ( "NO" )
n = 6
str_ = "GFGFGG"
findIfPossible(n, str_)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findIfPossible( int N, string str)
{
int countG = 0, countF = 0;
for ( int i = 0; i < N; i++) {
if (str[i] == 'G' )
countG++;
else
countF++;
}
if (2 * countF != countG) {
Console.WriteLine( "NO" );
}
else {
int id = 0;
bool flag = true ;
for ( int i = 0; i < N; i++) {
if (str[i] == 'G' ) {
countG--;
id++;
}
else {
countF--;
id--;
}
if (id < 0) {
flag = false ;
break ;
}
if (countG < countF) {
flag = false ;
break ;
}
}
if (flag) {
Console.WriteLine( "YES" );
}
else {
Console.WriteLine( "NO" );
}
}
}
public static void Main()
{
int n = 6;
string str = "GFGFGG" ;
findIfPossible(n, str);
}
}
|
Javascript
<script>
function findIfPossible(N, str)
{
let countG = 0, countF = 0;
for (let i = 0; i < N; i++)
{
if (str[i] == 'G' )
countG++;
else
countF++;
}
if (2 * countF != countG)
{
document.write( "NO" );
}
else
{
let id = 0;
let flag = true ;
for (let i = 0; i < N; i++)
{
if (str[i] == 'G' )
{
countG--;
id++;
}
else
{
countF--;
id--;
}
if (id < 0)
{
flag = false ;
break ;
}
if (countG < countF)
{
flag = false ;
break ;
}
}
if (flag)
{
document.write( "YES" );
}
else
{
document.write( "NO" );
}
}
}
let n = 6;
let str = "GFGFGG" ;
findIfPossible(n, str);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...