Check if a string can become empty by recursive deletion using Slicing Last Updated : 03 Jan, 2025 Comments Improve Suggest changes Like Article Like Report To check if the string "geeksforgeeks" can become empty by recursively deleting occurrences of a specific substring, we can use string slicing and recursion. Here's how to do it for the string "geeksforgeeks" with a specific substring, say "geeks".Let's understand this with the help of an example: Python def can_become_empty_with_slicing(string, substring): # If the string is empty, it can be considered successfully reduced to empty if not string: return True # Check if the substring exists at any position index = string.find(substring) if index != -1: # Remove the substring using slicing and recursively check return can_become_empty_with_slicing(string[:index] + string[index + len(substring):], substring) # If the substring is not found, and the string is not empty, return False return False # Example test case input_string = "geeksforgeeks" substring = "geeks" # Call the function and store the result result = can_become_empty_with_slicing(input_string, substring) print(result) OutputFalse Explanation:Base case: If the string becomes empty (i.e., not string), the function returns True, indicating that the string has successfully been reduced to an empty string.Recursive case: The function checks if the substring "geeks" exists in the string. If it does, it removes the first occurrence of the substring and recursively calls itself with the modified string.If the substring isn't found, the function returns False, indicating the string can't be reduced to empty by removing the substring. Comment More infoAdvertise with us Next Article Check if a string can become empty by recursive deletion using Slicing S Shashank Mishra Follow Improve Article Tags : Strings Python DSA python-string Practice Tags : pythonStrings 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 = "GEEGEEK 8 min read Check if a string consisting only of a, b, c can be made empty by removing substring "abc" recursively Given a string S size of N consisting of characters 'a', 'b', and 'c' only, the task is to check if the given string can be made empty by removing the string "abc" recursively or not. If found to be true, then print "Yes". Otherwise, print "No". Examples: Input: S = abcabcOutput: YesExplanation:Belo 7 min read Check if a string can be converted to another given string by removal of a substring Given two strings S and T of length N and M respectively, the task is to check if the string S can be converted to the string T by removing at most one substring of the string S. If found to be true, then print âYESâ. Otherwise, print âNOâ. Example: Input: S = âabcdefâ, T = âabcâ Output: YES Explana 7 min read Check if a string can be emptied by removing all subsequences of the form "10" Given a binary string str, the task is to check if the string can be emptied by removing all subsequences of the form "10" Examples: Input: str = "11011000"Output: YesExplanation:Following steps are required to be performed to empty the given string "11011000" ? "111000" ? "1100" ? "10" ? "" Input: 4 min read Check if a string can be made empty by repeatedly removing given subsequence Given a string str containing characters 'G' and 'F' only, the task is to check if the given string str can be made empty after removing all subsequences of the form "GFG". Examples: Input : N = 6, str[] = "GFGFGG"Output : YesExplanation : Two strings of "GFG" can be made with the first one with ind 7 min read Check if a string can be split into substrings starting with N followed by N characters Given a string str, the task is to check if it can be split into substrings such that each substring starts with a numeric value followed by a number of characters represented by that numeric integer. Examples: Input: str = "4g12y6hunter" Output: Yes Explanation: Substrings "4g12y" and "6hunter" sat 5 min read Check if String can be generated by concatenating character or String itself Given a target string S consisting of lowercase alphabets, the task is to make this string by performing some operation on an empty string such that: The first operation is to append a lower case alphabet to string S and The second operation is to append a copy of S to itself. Note: The first operat 6 min read Check if a string is a subsequence of another string ( using Stacks ) Given a string S, the task is to check if the string target is a subsequence of string S or not, using a Stack. Examples: Input: S = âKOTTAYAMâ, target = âKOTAâOutput: YesExplanation: âKOTAâ is a subsequence of âKOTTAYAMâ. Input: S = âGEEKSFORGEEKSâ, target =âFORFORâOutput: No Approach: Follow the s 9 min read Program for length of a string using recursion Given a string calculate length of the string using recursion. Examples: Input: str = "abcd"Output: 4Explanation: The string "abcd" has a length of 4.Input: str = "GEEKSFORGEEKS"Output: 13We have also covered this topicâfeel free to check it out as well5 Different methods to find length of a string 2 min read Check if 2 * K + 1 non-empty strings exists whose concatenation forms the given string Given a string S consisting of N characters and positive integer K, the task is to check if there exist any (K + 1) strings i.e., A1, A2, A3, ..., AK, A(K + 1) such that the concatenation of strings A1, A2, A3, ..., AK, and A(K + 1) and the concatenation of the reverse of each strings AK, A(K - 1), 6 min read Like