📌  相关文章
📜  通过递归删除给定的子字符串来检查字符串是否可以变为空

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

通过递归删除给定的子字符串来检查字符串是否可以变为空

给定一个字符串“str”和另一个字符串“sub_str”。我们可以从“str”中删除“sub_str”任意次数。还假设“sub_str”一次只出现一次。任务是通过一次又一次地删除“sub_str”来查找“str”是否可以变为空。
例子:

Input  : str = "GEEGEEKSKS", sub_str = "GEEKS"
Output : Yes
Explanation : In the string GEEGEEKSKS, we can first 
              delete the substring GEEKS from position 4.
              The new string now becomes GEEKS. We can 
              again delete sub-string GEEKS from position 1. 
              Now the string becomes empty.


Input  : str = "GEEGEEKSSGEK", sub_str = "GEEKS"
Output : No
Explanation : In the string it is not possible to make the
              string empty in any possible manner.

解决这个问题的一个简单方法是使用内置的字符串函数 find() 和 erase()。首先在原始字符串str中输入子字符串substr进行搜索,然后使用find()迭代原始字符串以查找子字符串的索引,返回原始字符串中子字符串的起始索引else -1 if未找到并使用 erase() 擦除该子字符串,直到原始字符串的长度大于 0。
上述简单的解决方案有效,因为给定的子字符串一次只出现一次。

C++
// C++ Program to check if a string can be
// converted to an empty string by deleting
// given sub-string from any position, any
// number of times.
#include
using namespace std;
 
// Returns true if str can be made empty by
// recursively removing sub_str.
bool canBecomeEmpty(string str, string sub_str)
{
    while (str.size() > 0)
    {
        // idx: to store starting index of sub-
        //      string found in the original string
        int idx = str.find(sub_str);
        if (idx == -1)
            break;
 
        // Erasing the found sub-string from
        // the original string
        str.erase(idx, sub_str.size());
    }
 
    return (str.size() == 0);
}
 
// Driver code
int main()
{
    string str = "GEEGEEKSKS", sub_str = "GEEKS";
    if (canBecomeEmpty(str, sub_str))
        cout<<"\nYes";
    else
        cout<<"\nNo";
    return 0;
}


Java
//Java program to check if a string can be
// converted to an empty string by deleting
// given sub-string from any position, any
// number of times.
 
class GFG {
 
// Returns true if str can be made empty by
// recursively removing sub_str.
    static boolean canBecomeEmpty(String str, String sub_str) {
        while (str.length() > 0) {
            // idx: to store starting index of sub-
            //      string found in the original string
            int idx = str.indexOf(sub_str);
            if (idx == -1) {
                break;
            }
 
            // Erasing the found sub-string from
            // the original string
            str = str.replaceFirst(sub_str,"");
        }
 
        return (str.length() == 0);
    }
 
// Driver code
    public static void main(String[] args) {
        String str = "GEEGEEKSKS", sub_str = "GEEKS";
        if (canBecomeEmpty(str, sub_str)) {
            System.out.print("\nYes");
        } else {
            System.out.print("\nNo");
        }
    }
}
// This code is contributed by 29AjayKumar


Python3
# Python3 program to check if a string can be
# converted to an empty string by deleting
# given sub-string from any position, any
# number of times.
 
# Returns true if str can be made empty by
# recursively removing sub_str.
def canBecomeEmpty(string, sub_str):
    while len(string) > 0:
 
        # idx: to store starting index of sub-
        #     string found in the original string
        idx = string.find(sub_str)
 
        if idx == -1:
            break
 
        # Erasing the found sub-string from
        # the original string
        string = string.replace(sub_str, "", 1)
 
    return (len(string) == 0)
 
# Driver code
if __name__ == "__main__":
    string = "GEEGEEKSKS"
    sub_str = "GEEKS"
    if canBecomeEmpty(string, sub_str):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by
# sanjeev2552


C#
// C# program to check if a string can be
// converted to an empty string by deleting
// given sub-string from any position, any
// number of times.
using System;
     
class GFG
{
 
    // Returns true if str can be made empty by
    // recursively removing sub_str.
    static Boolean canBecomeEmpty(String str, String sub_str)
    {
        while (str.Length > 0)
        {
            // idx: to store starting index of sub-
            //     string found in the original string
            int idx = str.IndexOf(sub_str);
            if (idx == -1)
            {
                break;
            }
 
            // Erasing the found sub-string from
            // the original string
            str = str.Replace(sub_str,"");
        }
 
        return (str.Length == 0);
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        String str = "GEEGEEKSKS", sub_str = "GEEKS";
        if (canBecomeEmpty(str, sub_str))
        {
            Console.Write("\nYes");
        }
        else
        {
            Console.Write("\nNo");
        }
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:

Yes