Check if a string can become empty by recursively deleting a given sub-string - GeeksforGeeks
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.
We strongly recommend you to minimize your browser and try this yourself first.
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.
Read full article from Check if a string can become empty by recursively deleting a given sub-string - GeeksforGeeks
No comments:
Post a Comment