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.
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.
We have existing solution for this problem please refer Check if a string can become empty by recursively deleting a given sub-string link. We will solve this problem in python using String Slicing. Approach is very simple,
- Use find() method of string to search given pattern sub-string.
- If sub-string lies in main string then find function will return index of it’s first occurrence.
- Now slice string in two parts, (i) from start of string till index-1 of founded sub-string, (ii) (start from first index of founded sub-string + length of sub-string) till end of string.
- Concatenate these two sliced part and repeat from step 1 until original string becomes empty or we don’t find sub-string anymore.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Check if a string can become empty by recursively deleting a given sub-string
- Meta Strings (Check if two strings can become same after a swap in one string)
- Python dictionary, set and counter to check if frequencies can become same
- String slicing in Python to rotate a string
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Check if frequency of all characters can become same by one removal
- Python | Get the substring from given string using list slicing
- Python | Reverse Slicing of given string
- String Slicing in Python
- Python | Reverse Interval Slicing String
- Python | Reverse Incremental String Slicing
- Basic Slicing and Advanced Indexing in NumPy Python
- Python Slicing | Reverse an array in groups of given size
- Program to cyclically rotate an array by one in Python | List Slicing
- Python Slicing | Extract ‘k’ bits from a given position
- Python | Slicing list from Kth element to last element
- Python | Variable list slicing
- Python | Alternate range slicing in list
- Python | Custom slicing in List
- Python | K elements Slicing
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : rakeshsshankala