Check if a string can become empty by recursively deleting a given sub-string - GeeksforGeeks

16 Jun, 2021
Given a string “str” and another string “sub_str”. We are allowed to delete “sub_str” from “str” any number of times. It is also given that the “sub_str” appears only once at a time. The task is to find if “str” can become empty by removing “sub_str” again and again.Examples:  
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.
 
A simple solution to solve this problem is by using inbuilt string functions find() and erase(). First input the sub-string substr for searching purpose in the original string str, then iterate the original string to find the index of sub-string using find() which return starting index of the sub-string in the original string else -1 if not found and erase that sub-string using erase() until length of original string is greater than 0.The above simple solutions works because the given substring appears only once at a time.  
C++
Java
Python3
C#
Javascript
// 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<bits/stdc++.h>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 codeint main(){    string str = "GEEGEEKSKS", sub_str = "GEEKS";    if (canBecomeEmpty(str, sub_str))        cout<<"\nYes";    else        cout<<"\nNo";    return 0;}
//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 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 codeif __name__ == "__main__":    string = "GEEGEEKSKS"    sub_str = "GEEKS"    if canBecomeEmpty(string, sub_str):        print("Yes")    else:        print("No") # This code is contributed by# sanjeev2552
// 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
<script>// Javascript 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_strfunction canBecomeEmpty(str,sub_str){    while (str.length > 0)    {                 // idx: to store starting index of sub-            //      string found in the original string            let 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 codelet str = "GEEGEEKSKS", sub_str = "GEEKS";if (canBecomeEmpty(str, sub_str)) {    document.write("<br>Yes");} else {    document.write("<br>No");}  // This code is contributed by rag2127</script>
Output:  
Yes
This article is contributed by Himanshu Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
29AjayKumar
sanjeev2552
rag2127
STL
Strings
Strings
STL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python program to check if a string is palindrome or not
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++
Longest Palindromic Substring | Set 1
Caesar Cipher in Cryptography
Check whether two strings are anagram of each other
Top 50 String Coding Problems for Interviews
Length of the longest substring without repeating characters
Reverse words in a given string
How to split a string in C/C++, Python and Java?