Python – Equidistant consecutive characters Strings
Last Updated :
23 Apr, 2023
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”, “mpsw”, “abd”]
Output : [‘abcd’]
Explanation : In abcd, consecutive characters are at distance 1.
Method #1 : Using list comprehension + all()
In this, we check for each character has a common difference using all(), and list comprehension is used to iterate strings.
Python3
test_list = [ "abcd" , "egil" , "mpsv" , "abd" ]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if all ( ord (
sub[idx + 1 ]) - ord (sub[idx]) = = ord (sub[ 1 ]) - ord (sub[ 0 ]) for idx in range ( 0 , len (sub) - 1 ))]
print ( "Filtered Strings : " + str (res))
|
Output
The original list is : ['abcd', 'egil', 'mpsv', 'abd']
Filtered Strings : ['abcd', 'mpsv']
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #2 : Using filter() + lambda + ord() + all()
In this, we perform the task of filtering using filter() and lambda function. The task of ord() is to get ASCII equivalent of each character.
Python3
test_list = [ "abcd" , "egil" , "mpsv" , "abd" ]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if all ( ord (sub[idx + 1 ]) - ord (sub[idx]) = = ord (sub[ 1 ]) - ord (sub[ 0 ]) for idx in range ( 0 , len (sub) - 1 ))]
print ( "Filtered Strings : " + str (res))
|
Output
The original list is : ['abcd', 'egil', 'mpsv', 'abd']
Filtered Strings : ['abcd', 'mpsv']
Time Complexity: O(n2)
Auxiliary Space: O(n)
Another approach using set
Here, we first check if the length of the string is less than 3, then we can’t check for equidistant characters, hence we return False.
Then, we calculate the difference between the first two characters and store it in a set.
Next, we iterate through the rest of the string and check if the difference between the current character and the previous character is equal to the first calculated difference, if not we return False.
If the loop finishes without returning False, then we return True as all characters have a common difference.
Python3
def equidistant(string):
if len (string) < 3 :
return False
diff = ord (string[ 1 ]) - ord (string[ 0 ])
diff_set = {diff}
for i in range ( 1 , len (string) - 1 ):
curr_diff = ord (string[i + 1 ]) - ord (string[i])
if curr_diff not in diff_set:
return False
return True
test_list = [ "abcd" , "egil" , "mpsv" , "abd" ]
print ( "The original list is : " + str (test_list))
res = list ( filter (equidistant, test_list))
print ( "Filtered Strings : " + str (res))
|
Output
The original list is : ['abcd', 'egil', 'mpsv', 'abd']
Filtered Strings : ['abcd', 'mpsv']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#3: Using Recursive method.
Algorithm:
- Define a function is_equidistant(s) to check if a given string s is equidistant, i.e. if the difference between ASCII values of consecutive characters is the same.
- The function checks if the length of the string is less than or equal to 2. If so, it returns True.
- Otherwise, it returns the boolean result of comparing the differences between ASCII values of the first two characters and the second two characters, and calling itself recursively on the string without the first character.
- Define a function filter_equidistant_strings(test_list) to filter equidistant strings from the given list test_list.
- If the list is empty, return an empty list.
- If the first string in the list is equidistant, return a list consisting of that string concatenated with the result of calling the function recursively on the rest of the list.
- Otherwise, return the result of calling the function recursively on the rest of the list.
- Call the function filter_equidistant_strings() on the given test_list.
- Print the filtered list.
Python3
def is_equidistant(s):
if len (s) < = 2 :
return True
else :
return ord (s[ 1 ]) - ord (s[ 0 ]) = = ord (s[ 2 ]) - ord (s[ 1 ]) and is_equidistant(s[ 1 :])
def filter_equidistant_strings(test_list):
if not test_list:
return []
elif is_equidistant(test_list[ 0 ]):
return [test_list[ 0 ]] + filter_equidistant_strings(test_list[ 1 :])
else :
return filter_equidistant_strings(test_list[ 1 :])
test_list = [ "abcd" , "egil" , "mpsv" , "abd" ]
print ( "The original list is : " + str (test_list))
res = filter_equidistant_strings(test_list)
print ( "Filtered Strings : " + str (res))
|
Output
The original list is : ['abcd', 'egil', 'mpsv', 'abd']
Filtered Strings : ['abcd', 'mpsv']
Time complexity: The is_equidistant() function is called on each string in the list, which takes O(n) time for a string of length n. Therefore, the time complexity of filter_equidistant_strings() is O(n^2) in the worst case. However, if the average length of strings in the list is small, the time complexity will be closer to O(n).
Auxiliary Space: The recursive implementation uses O(n) space due to the recursive calls on each string in the list.
Method#4: Using the reduce() function:
Algorithm:
1.Initialize the list of strings.
2.Use filter function along with lambda function and apply reduce function for each string in the list.
3.The lambda function checks if the difference between ASCII value of consecutive characters is equal to the difference between ASCII value of first two characters.
4.reduce() function will return True if all the elements in the iterable are True, else it will return False.
5.If the result of the lambda function is True for a string, then filter function will include that string in the result list.
Print the result list.
Python3
from functools import reduce
test_list = [ "abcd" , "egil" , "mpsv" , "abd" ]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda x: reduce ( lambda a,b: a and b, [ ord (x[i + 1 ]) - ord (x[i]) = = ord (x[ 1 ]) - ord (x[ 0 ]) for i in range ( len (x) - 1 )]), test_list))
print ( "Filtered Strings : " + str (res))
|
Output
The original list is : ['abcd', 'egil', 'mpsv', 'abd']
Filtered Strings : ['abcd', 'mpsv']
Time complexity: O(n*m), where n is the number of strings in the list and m is the maximum length of the string in the list.
Auxiliary Space: O(k), where k is the length of the longest string in the list. This is because we are storing the result in a list.
Method #5: Using for loop
Algorithm
- Initialize an input list of strings.
- Create an empty list to store the filtered strings.
- Iterate over each string in the input list.
- For each string, iterate over each pair of consecutive characters and check if the difference between them is the same as the difference between the first two characters.
- If the difference between any pair of consecutive characters is not the same, then the string is not arithmetic and move on to the next string.
- If all pairs of consecutive characters have the same difference, then the string is arithmetic and add it to the result list.
- Print the result list.
Python3
test_list = [ "abcd" , "egil" , "mpsv" , "abd" ]
print ( "The original list is : " + str (test_list))
res = []
for x in test_list:
is_arithmetic = True
for i in range ( len (x) - 1 ):
if ord (x[i + 1 ]) - ord (x[i]) ! = ord (x[ 1 ]) - ord (x[ 0 ]):
is_arithmetic = False
break
if is_arithmetic:
res.append(x)
print ( "Filtered Strings : " + str (res))
|
Output
The original list is : ['abcd', 'egil', 'mpsv', 'abd']
Filtered Strings : ['abcd', 'mpsv']
The time complexity of this code is O(n * k), where n is the number of strings in the input list, and k is the length of the longest string. The outer loop iterates over n strings, while the inner loop iterates over k-1 pairs of consecutive characters in each string. The ord() function, which returns the ASCII value of a character, takes constant time.
The auxiliary space of this code is O(n), where n is the number of strings in the input list. The space required for the input list and the output list is proportional to the number of strings in the list. The space required for the variables used inside the loop is constant, regardless of the length of the strings. Therefore, the space complexity of the code is proportional to the size of the input list.
Method 7 : uses the zip() function and a set comprehension
steps :
Initialize a list test_list with strings as its elements.
Print the original list using print(“The original list is : ” + str(test_list)).
Initialize an empty list res to store the filtered strings.
Iterate over each string x in test_list using a for loop.
Use the zip() function to create a list of pairs of adjacent characters in the string x.
Calculate the set of differences between adjacent ASCII codes in the string x by subtracting the ASCII code of the first character from the ASCII code of the second character using a set comprehension {ord(y) – ord(x) for x, y in zip(x, x[1:])}. The set comprehension calculates the difference for each pair of adjacent characters in the string x and removes duplicates to obtain a set of unique differences.
Check if the length of the set of differences is equal to 1 using the len() function. If the length is 1, then all the differences are the same, and the string x satisfies the condition.
If the string x satisfies the condition, append it to the list res using the append() method.
After all strings in test_list have been processed, print the filtered strings using print(“Filtered Strings : ” + str(res)).
Python3
test_list = [ "abcd" , "egil" , "mpsv" , "abd" ]
print ( "The original list is : " + str (test_list))
res = []
for x in test_list:
diffs = { ord (y) - ord (x) for x, y in zip (x, x[ 1 :])}
if len (diffs) = = 1 :
res.append(x)
print ( "Filtered Strings : " + str (res))
|
Output
The original list is : ['abcd', 'egil', 'mpsv', 'abd']
Filtered Strings : ['abcd', 'mpsv']
The time complexity of this approach is O(NM), where N is the length of test_list and M is the length of the longest string in test_list.
The space complexity of this approach is also O(NM), because the set of differences is stored in memory for each string that is added to the result list res.
Similar Reads
Python | Pair the consecutive character strings in a list
Sometimes while programming, we can face a problem in which we need to perform consecutive element concatenation. This problem can occur at times of school programming or competitive programming. Let's discuss certain ways in which this problem can be solved. Method #1 : Using list comprehension + z
5 min read
Consecutive characters frequency - Python
This problem involves identifying characters that appear consecutively and counting how many times they appear together. Here, we will explore different methods to calculate the frequency of consecutive characters in a string. Using regular expressionsWe can use the re module to efficiently count co
3 min read
Python - K length consecutive characters
Given a String, extract all the K-length consecutive characters. Input : test_str = 'geekforgeeeksss is bbbest forrr geeks', K = 3 Output : ['eee', 'sss', 'bbb', 'rrr'] Explanation : K length consecutive strings extracted. Input : test_str = 'geekforgeeeksss is bbbest forrrr geeks', K = 4 Output : [
4 min read
Python - Custom Consecutive character repetition in String
Given a String, repeat characters consecutively by number mapped in dictionary. Input : test_str = 'Geeks4Geeks', test_dict = {"G" : 3, "e" : 1, "4" : 3, "k" : 5, "s" : 3} Output : GGGeekkkkksss444GGGeekkkkksss Explanation : Each letter repeated as per value in dictionary.Input : test_str = 'Geeks4G
4 min read
Python | Split string in groups of n consecutive characters
Given a string (be it either string of numbers or characters), write a Python program to split the string by every nth character. Examples: Input : str = "Geeksforgeeks", n = 3 Output : ['Gee', 'ksf', 'org', 'eek', 's'] Input : str = "1234567891234567", n = 4 Output : [1234, 5678, 9123, 4567] Method
2 min read
Python - Consecutive Repetition of Characters
Sometimes, while working with character lists we can have a problem in which we need to perform consecutive repetition of characters. This can have applications in many domains. Let us discuss certain ways in which this task can be performed. Method #1: Using list comprehension This is one of the
5 min read
Python | Custom Consecutive Character Pairing
Sometimes, while working with Python Strings, we can have problem in which we need to perform the pairing of consecutive strings with deliminator. This can have application in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using join() + list comprehension T
4 min read
Python - Characters occurring in multiple Strings
Sometimes, while working with Python strings, we can have problem in which we need to extract the characters which have occurrences in more than one string in string list. This kind of problem usually occurs in web development and Data Science domains. Lets discuss certain ways in which this task ca
5 min read
Python - Least Frequent Character in String
The task is to find the least frequent character in a string, we count how many times each character appears and pick the one with the lowest count. Using collections.CounterThe most efficient way to do this is by using collections.Counter which counts character frequencies in one go and makes it ea
3 min read
Python | Consecutive String Comparison
Sometimes, while working with data, we can have a problem in which we need to perform comparison between a string and it's next element in a list and return all strings whose next element is similar list. Let's discuss certain ways in which this task can be performed. Method #1 : Using zip() + loop
3 min read