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 - Merge consecutive empty Strings Sometimes, while working with python lists, we can have a problem in which we need to perform a merge operation on a string of lists. This merge is consecutive to convert multiple spaces to one. Let's discuss a certain way in which this can be performed. Method #1 : Using loop, This is a brute way i
6 min read
Python - First K consecutive digits in String Given a String and number K, extract first K consecutive digits making number. Input : test_str = "geeks5geeks43best", K = 2 Output : 43 Explanation : 43 is first 2 consecutive digits. Input : test_str = "geeks5gee2ks439best", K = 3 Output : 439 Explanation : 439 is first 3 consecutive digits. Metho
5 min read
Find Longest Consecutive Letter and Digit Substring - Python The task is to identify the longest sequence of consecutive letters or digits in a given string. A consecutive letter or digit refers to a substring where each character is adjacent to the next one in the alphabet for letters or numerically for digits. It involves identifying the longest sequence of
4 min read
Python - Remove Consecutive K element records Sometimes, while working with Python records, we can have a problem in which we need to remove records on the basis of presence of consecutive K elements in tuple. This kind of problem is peculiar but can have applications in data domains. Let's discuss certain ways in which this task can be perform
7 min read
Python - Equidistant consecutive characters Strings Given a Strings List, extract all the strings, whose consecutive characters are at the common difference in ASCII order. Input : test_list = ["abcd", "egil", "mpsv", "abd"] Output : ['abcd', 'mpsv'] Explanation : In mpsv, consecutive characters are at distance 3. Input : test_list = ["abcd", "egil",
9 min read
Python - Maximum Consecutive Substring Occurrence Sometimes, while working with Python, we can have a problem in which we must check for substrings occurring in consecutive repetition. This can have applications in data domains. Let us discuss a way in which this task can be performed. Method #1: Using max() + re.findall() A combination of the abov
5 min read
Python - Delete elements in range In Python, we often need to remove elements from a list within a specific range. This can be useful when cleaning data or modifying lists based on certain conditions. Using List Comprehension List comprehension is one of the most Pythonic and efficient ways to filter out elements. It is concise, eas
3 min read
Python - Selective consecutive Suffix Join Given a list of elements, the task is to write a Python program to perform a join of consecutive strings according to the suffix of each string. Input : test_list = ["Geeks-", "for-", "Geeks", "is", "best-", "for", "geeks", suff = '-' Output : ['Geeks-for-Geeks', 'is', 'best-for', 'geeks'] Explanati
2 min read
Python - Incremental Size Chunks from Strings Given a String, split it into incremental sizes consecutive list. Input : test_str = 'geekforgeeks is best' Output : ['g', 'ee', 'kfo', 'rgee', 'ks is', ' best'] Explanation : Characters size increasing in list. Input : test_str = 'geekforgeeks' Output : ['g', 'ee', 'kfo', 'rgee', 'ks'] Explanation
3 min read
List of strings in Python A list of strings in Python stores multiple strings together. In this article, weâll explore how to create, modify and work with lists of strings using simple examples.Creating a List of StringsWe can use square brackets [] and separate each string with a comma to create a list of strings.Pythona =
2 min read