Python - Consecutive element deletion strings
Last Updated :
13 Apr, 2023
Sometimes, while working with Python, we can have a problem in which we have a string and wish to extract all possible combination of words after deletion of consecutive elements one at a time. This can have application in many domains. Lets discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + list slicing This is one of the way in which this task can be performed. In this, we iterate for the elements of list and keep on creating new string using consecutive list slicing.
Python3
# Python3 code to demonstrate working of
# Consecutive element deletion strings
# Using list comprehension + list slicing
# initializing string
test_str = 'Geeks4Geeks'
# printing original string
print("The original string is : " + str(test_str))
# Consecutive element deletion strings
# Using list comprehension + list slicing
res = [test_str[: idx] + test_str[idx + 1:]
for idx in range(len(test_str))]
# printing result
print("Consecutive Elements removal list : " + str(res))
Output :
The original string is : Geeks4Geeks Consecutive Elements removal list : ['eeks4Geeks', 'Geks4Geeks', 'Geks4Geeks', 'Gees4Geeks', 'Geek4Geeks', 'GeeksGeeks', 'Geeks4eeks', 'Geeks4Geks', 'Geeks4Geks', 'Geeks4Gees', 'Geeks4Geek']
Time Complexity: O(n), where n is the length of the input list. This is because we’re using list comprehension + list slicing which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #2 : Using list comprehension + enumerate() The combination of above methods can be used to perform this task. In this, we extract the index using enumerate. This gives more cleaner code.
Python3
# Python3 code to demonstrate working of
# Consecutive element deletion strings
# Using list comprehension + enumerate()
# initializing string
test_str = 'Geeks4Geeks'
# printing original string
print("The original string is : " + str(test_str))
# Consecutive element deletion strings
# Using list comprehension + enumerate()
res = [test_str[:idx] + test_str[idx + 1:]
for idx, _ in enumerate(test_str)]
# printing result
print("Consecutive Elements removal list : " + str(res))
Output :
The original string is : Geeks4Geeks Consecutive Elements removal list : ['eeks4Geeks', 'Geks4Geeks', 'Geks4Geeks', 'Gees4Geeks', 'Geek4Geeks', 'GeeksGeeks', 'Geeks4eeks', 'Geeks4Geks', 'Geeks4Geks', 'Geeks4Gees', 'Geeks4Geek']
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Method 3 Using a for loop.
Algorithm:
- Take input string test_str and its length n.
- Initialize an empty list res.
- Using a for loop, iterate over the range of n.
- In each iteration, append the string obtained by removing the character at that index to res.
- Return the res list.
Python3
# initializing string
test_str = 'Geeks4Geeks'
# printing original string
print("The original string is : " + str(test_str))
# Consecutive element deletion strings
# Using a loop
res = []
for i in range(len(test_str)):
res.append(test_str[:i] + test_str[i+1:])
# printing result
print("Consecutive Elements removal list : " + str(res))
#this code contributed by tvsk
OutputThe original string is : Geeks4Geeks
Consecutive Elements removal list : ['eeks4Geeks', 'Geks4Geeks', 'Geks4Geeks', 'Gees4Geeks', 'Geek4Geeks', 'GeeksGeeks', 'Geeks4eeks', 'Geeks4Geks', 'Geeks4Geks', 'Geeks4Gees', 'Geeks4Geek']
Time Complexity: O(n^2), where n is the length of test_str.
Auxiliary Space: O(n), where n is the length of test_str. This is the space taken up by the res list.
Method 4: Using map() and lambda function
Algorithm
- Initialize the string test_str.
- Define a lambda function that takes a character in the string and returns the string with that character removed.
- Use map() to apply the lambda function to each character in the string and return a list of strings.
- Print the resultant list of strings.
Python3
# initializing string
test_str = 'Geeks4Geeks'
# printing original string
print("The original string is : " + str(test_str))
# Consecutive element deletion strings
# Using map() and lambda function
res = list(map(lambda x: test_str[:x] + test_str[x+1:], range(len(test_str))))
# printing result
print("Consecutive Elements removal list : " + str(res))
#This code is contributed By Vinay Pinjala.
OutputThe original string is : Geeks4Geeks
Consecutive Elements removal list : ['eeks4Geeks', 'Geks4Geeks', 'Geks4Geeks', 'Gees4Geeks', 'Geek4Geeks', 'GeeksGeeks', 'Geeks4eeks', 'Geeks4Geks', 'Geeks4Geks', 'Geeks4Gees', 'Geeks4Geek']
Time Complexity:
The lambda function takes constant time O(1) to execute.
The map() function applies the lambda function to each character in the string, which takes O(n) time, where n is the length of the string.
Therefore, the overall time complexity of this algorithm is O(n), where n is the length of the string.
Auxiliary Space: O(n), where n is the length of the string, since we are storing the resultant list of strings in memory
Method 5: Using itertools and combinations:
1. First, we initialize the input string as test_str = 'Geeks4Geeks'.
2. We print the original string.
3. We create an empty list res to store the results.
4. We iterate over the indices of the string using range(len(test_str)).
5. For each index i, we create a new string by concatenating the substring before i with the substring after i+1, and append it to the res list.
6. After the loop finishes, we print the res list which contains all the strings obtained by deleting consecutive characters in the input string.
Python3
from itertools import combinations
# initializing string
test_str = 'Geeks4Geeks'
# printing original string
print("The original string is : " + str(test_str))
# Consecutive element deletion strings
# Using combinations from itertools
res = ["".join(c) for i in range(1, len(test_str)) for c in combinations(test_str, i)]
# printing result
print("Consecutive Elements removal list : " + str(res))
#This code is contributed by Jyothi pinjala
Output...ek4Geeks', 'es4Geeks', 'eks4Geek', 'eks4Gees', 'eks4Geks', 'eks4Geks', 'eks4eeks', 'eksGeeks', 'ek4Geeks', 'es4Geeks', 'ks4Geeks', 'Geeks4Gee', 'Geeks4Gek', 'Geeks4Ges', 'Geeks4Gek', 'Geeks4Ges', 'Geeks4Gks', 'Geeks4eek', 'Geeks4ees', 'Geeks4eks', 'Geeks4eks', 'GeeksGeek', 'GeeksGees', 'GeeksGeks', 'GeeksGeks', 'Geekseeks', 'Geek4Geek', 'Geek4Gees', 'Geek4Geks', 'Geek4Geks', 'Geek4eeks', 'GeekGeeks', 'Gees4Geek', 'Gees4Gees', 'Gees4Geks', 'Gees4Geks', 'Gees4eeks', 'GeesGeeks', 'Gee4Geeks', 'Geks4Geek', 'Geks4Gees', 'Geks4Geks', 'Geks4Geks', 'Geks4eeks', 'GeksGeeks', 'Gek4Geeks', 'Ges4Geeks', 'Geks4Geek', 'Geks4Gees', 'Geks4Geks', 'Geks4Geks', 'Geks4eeks', 'GeksGeeks', 'Gek4Geeks', 'Ges4Geeks', 'Gks4Geeks', 'eeks4Geek', 'eeks4Gees', 'eeks4Geks', 'eeks4Geks', 'eeks4eeks', 'eeksGeeks', 'eek4Geeks', 'ees4Geeks', 'eks4Geeks', 'eks4Geeks', 'Geeks4Geek', 'Geeks4Gees', 'Geeks4Geks', 'Geeks4Geks', 'Geeks4eeks', 'GeeksGeeks', 'Geek4Geeks', 'Gees4Geeks', 'Geks4Geeks', 'Geks4Geeks', 'eeks4Geeks']
Time complexity: O(n^2), where n is the length of the input string. The outer loop runs n times, and for each iteration of the outer loop, the slice operation runs in O(n) time. Thus, the overall time complexity is O(n^2).
Space complexity: O(n^2), since the resulting list res will contain n*(n-1)/2 strings, which is O(n^2). The space complexity of the slice operation is O(n), but since it is performed n*(n-1)/2 times, the overall space complexity is O(n^2).
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Enumerate() in Python
enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Introduction
Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Input and Output in Python
Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read