📌  相关文章
📜  检查是否可以通过重复删除给定的子序列来使字符串为空

📅  最后修改于: 2022-05-13 01:57:08.283000             🧑  作者: Mango

检查是否可以通过重复删除给定的子序列来使字符串为空

给定一个仅包含字符'G''F'的字符串str ,任务是检查给定字符串str是否可以在删除所有“GFG”形式的子序列后为空。

例子:

做法:可以看出,要制作GFG,需要2个G ,中间有1个F ,所以如果G的个数不等于F个数的两倍,就永远不可能做出一些个数GFG从给定的字符串完成。还有一点要检查的是,后面的G的个数绝对不能小于 F 的个数,因为字符串是GGGFFGFGFFG,所以可以看出最后两个, FF只有一个G ,所以不会有可能从它们两者中生成 GFG,因此我们的答案将再次是否定的。最后要检查的条件是,通过的 G 计数永远不会小于 F 的计数,以便生成 GFG。请按照以下步骤解决问题:

  • 将变量countGcountF初始化为0 ,将GF的计数存储在字符串str[]中。
  • 使用变量i迭代范围[0, N]并执行以下步骤:
    • 如果str[i]等于G,则将countG的值增加1,否则,将countF的值增加1。
  • 如果2*countF不等于countG,则不可能打印“NO”并返回
  • 将变量id初始化为0以计算GF的计数,直到特定索引和变量标志true以存储答案。
  • 使用变量i迭代范围[0, N]并执行以下步骤:
    • 如果str[i]等于'1',则将countG的值减1 ,并将id的值加1。
    • 否则,将countF的值减1 ,将id的值减1。
    • 如果id小于0,那么额外的G最终将不会被使用,因此将flag的值设置为false并中断。
    • 如果countG小于countF ,那么额外的F最终将不会被使用,因此将flag的值设置为false并中断。
  • 如果flag等于true ,则打印“YES”,否则打印“NO”。

下面是上述方法的实现。

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if a string can be
// made empty by removing all
// subsequences of the form "GFG" or not
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";
        }
    }
}
 
// Driver Code
int main()
{
    int n = 6;
    string str = "GFGFGG";
 
    findIfPossible(n, str);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to check if a string can be
    // made empty by removing all
    // subsequences of the form "GFG" or not
    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);
    }
}
 
// This code is contributed by maddler.


Python3
# Python3 program for the above approach
 
# Function to check if a string can be
# made empty by removing all subsequences
# of the form "GFG" or not
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")
 
# Driver Code
n = 6
str_ = "GFGFGG"
 
findIfPossible(n, str_)
 
# This code is contributed by Parth Manchanda


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
// Function to check if a string can be
// made empty by removing all
// subsequences of the form "GFG" or not
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");
        }
    }
}
 
// Driver Code
public static void Main()
{
    int n = 6;
    string str = "GFGFGG";
    findIfPossible(n, str);
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出:
YES

时间复杂度: O(N)
辅助空间: O(1)