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 info S Shashank Mishra Follow Improve Article Tags : DSA python-string Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 14 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 6 min read Problem of The Day - Develop the Habit of Coding 5 min read Like