Open In App

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

Last Updated : 06 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Implementation:

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 'false'
 
    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)



Similar Reads

Check if a string can become empty by recursively deleting a given sub-string
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 :
8 min read
Check if L sized Subarray of first N numbers can have sum S with one deletion allowed
Consider an integer sequence A = {1, 2, 3, ...., N} i.e. the first N natural numbers in order and two integers, L and S. Check whether there exists a subarray of length L and sum S after removing at most one element from A. Examples: Input: N = 5, L = 3, S = 11Output: YESExplanation: We can remove 3 from A to obtain A = {1, 2, 4, 5} where {2, 4, 5}
6 min read
String slicing in Python to Rotate a String
Given a string of size n, write functions to perform following operations on string. Left (Or anticlockwise) rotate the given string by d elements (where d <= n).Right (Or clockwise) rotate the given string by d elements (where d <= n).Examples: Input : s = "GeeksforGeeks" d = 2Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeek
3 min read
Meta Strings (Check if two strings can become same after a swap in one string)
Given two strings, the task is to check whether these strings are meta strings or not. Meta strings are the strings which can be made equal by exactly one swap in any of the strings. Equal string are not considered here as Meta strings. Examples: Input : str1 = "geeks" str2 = "keegs"Output : YesBy just swapping 'k' and 'g' in any of string, both wi
8 min read
Python dictionary, set and counter to check if frequencies can become same
Given a string which contains lower alphabetic characters, we need to remove at most one character from this string in such a way that frequency of each distinct character becomes same in the string. Examples: Input : str = “xyyz” Output : Yes We can remove character ’y’ from above string to make the frequency of each character same. Input : str =
2 min read
Number of days after which tank will become empty
Given a tank with capacity C liters which is completely filled in starting. Everyday tank is filled with l liters of water and in the case of overflow extra water is thrown out. Now on i-th day i liters of water is taken out for drinking. We need to find out the day at which tank will become empty the first time. Examples: Input : Capacity = 5 l =
11 min read
Minimize deletions to reduce String to size 1 wherein ith deletion, N/2^i occurrences of (X+i-1)th character can be deleted
Given string str of length N and a character X, where N is always of the form 2k, the task is to find the minimum replacement operations required to reduce the size of the string to 1 wherein ith deletion, N/2i occurrences of (X + i - 1)th character can be deleted either from the front or the back of the string. Example: Input: str = "CDAAAABB", X
7 min read
Python | Get the substring from given string using list slicing
Given a string, write a Python program to get the substring from given string using list slicing. Let’s try to get this using different examples. What is substring? A substring is a portion of a string. Python offers a variety of techniques for producing substrings, as well as for determining the index of a substring and more. Syntax of list slicin
4 min read
Python | Reverse Slicing of given string
Sometimes, while working with strings we might have a problem in which we need to perform the reverse slicing of string, i.e slicing the string for certain characters from the rear end. Let's discuss certain ways in which this can be done. Method #1 : Using join() + reversed() The combination of above function can be used to perform this particular
5 min read
String Slicing in Python
Python slicing is about obtaining a sub-string from the given string by slicing it respectively from start to end. How String slicing in Python works For understanding slicing we will use different methods, here we will cover 2 methods of string slicing, one using the in-build slice() method and another using the [:] array slice. String slicing in
4 min read
Article Tags :
Practice Tags :