The Wayback Machine - https://web.archive.org/web/20220710234427/https://www.geeksforgeeks.org/string-slicing-python-check-string-can-become-empty-recursive-deletion/
Skip to content
Related Articles

Related Articles

String slicing in Python to check if a string can become empty by recursive deletion

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 02 Jun, 2022

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 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,

  1. Use find() method of string to search given pattern sub-string.
  2. If sub-string lies in main string then find function will return index of it’s first occurrence.
  3. 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.
  4. Concatenate these two sliced part and repeat from step 1 until original string becomes empty or we don’t find sub-string anymore.

Python3




def checkEmpty(input, pattern):
       
    # If both are empty 
    if len(input)== 0 and len(pattern)== 0:
         return 'true'
   
    # If only pattern is empty
    if len(pattern)== 0:
         return 'true'
   
    while (len(input) != 0):
 
        # find sub-string in main string
        index = input.find(pattern)
 
        # check if sub-string founded or not
        if (index ==(-1)):
          return 'false'
 
        # slice input string in two parts and concatenate
        input = input[0:index] + input[index + len(pattern):]             
 
    return 'true'
   
# Driver program
if __name__ == "__main__":
    input ='GEEGEEKSKS'
    pattern ='GEEKS'
    print (checkEmpty(input, pattern))

Output:

true

Time Complexity: O(n/m), where n is the length of string, and m is the length of substring

Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!