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.
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.
This article is contributed by Himanshu Gupta. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- String slicing in Python to check if a string can become empty by recursive deletion
- Decode a string recursively encoded as count followed by substring
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Count occurrences of a substring recursively
- Minimum steps to delete a string by deleting substring comprising of same characters
- Meta Strings (Check if two strings can become same after a swap in one string)
- Recursively print all sentences that can be formed from list of word lists
- 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
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Check if string can be made lexicographically smaller by reversing any substring
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Find largest word in dictionary by deleting some characters of given string
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Check if the given string is shuffled substring of another string
- Recursively remove all adjacent duplicates
- Determine the winner of a game of deleting Characters from a String
- Largest string obtained in Dictionary order after deleting K characters