Open In App

Python – Consecutive Alphabetic Occurrence

Last Updated : 17 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Strings, we can have a problem in which we need to check whether we can find occurrence of characters consecutive and according to English alphabets. This kind of problem can occur in school programming and day-day programming. Lets discuss certain ways in which this task can be performed. 

Method #1 : Using loop + ascii_letters + zip() The combination of above methods can be used to perform this task. In this, we extract the English alphabets using ascii_letters and check for consecution using zip(). 

Python3




# Python3 code to demonstrate working of
# Consecutive Alphabetic Occurrence
# Using loop + ascii_letters + zip()
from string import ascii_letters
 
# initializing string
test_str = 'geeksforgeeks is best fgr geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# Consecutive Alphabetic Occurrence
# Using loop + ascii_letters + zip()
res = []
for i, j in zip(ascii_letters, ascii_letters[1:]) :
    if i + j in test_str:
        res.append((i, j))
 
# printing result
print("The Consecutive matching letter pairs : " + str(res))


Output : 

The original string is : geeksforgeeks is best fgr geeks The Consecutive matching letter pairs : [(‘f’, ‘g’), (‘s’, ‘t’)]

  Method #2 : Using list comprehension + ascii_letters + zip() The combination of above methods can be used to perform this task. In this, we perform similar way as above just in one-liner shortened way using list comprehension. 

Python3




# Python3 code to demonstrate working of
# Consecutive Alphabetic Occurrence
# Using list comprehension + ascii_letters + zip()
from string import ascii_letters
 
# initializing string
test_str = 'geeksforgeeks is best fgr geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# Consecutive Alphabetic Occurrence
# Using list comprehension + ascii_letters + zip()
res = [(i, j) for i, j in zip(ascii_letters,
      ascii_letters[1:]) if i + j in test_str]
 
# printing result
print("The Consecutive matching letter pairs : " + str(res))


Output : 

The original string is : geeksforgeeks is best fgr geeks
The Consecutive matching letter pairs : [('f', 'g'), ('s', 't')]

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(n)

Space Complexity: O(n)

Approach#3: Using Simple Intuition

In this approach, we iterate over the string and check each pair of adjacent characters to see if they form a consecutive alphabetic occurrence. If so, we add the pair to a list and return it.

Algorithm

1. Create an empty list to store all consecutive matching letter pairs.
2. Iterate over the string:
a. If the current character and the next character are consecutive alphabetically, append the pair to the list.
3. Return the list of consecutive matching letter pairs.

Python3




def consecutive_alphabetic_occurrence(s):
    pairs = []
    for i in range(len(s)-1):
        if ord(s[i+1]) == ord(s[i])+1:
            pairs.append((s[i], s[i+1]))
    return pairs
s='geeksforgeeks is best fgr geeks'
print(consecutive_alphabetic_occurrence(s))


Output

[('s', 't'), ('f', 'g')]

Time complexity: O(n) because we are iterating over the string once.

Auxiliary Space: O(m) where m is the number of consecutive matching letter pairs in the string.

Approach#4: Using filter+lambda

This approach generates all pairs of consecutive characters in the given string and filters the pairs that consist of consecutive letters. Finally, it returns the list of consecutive matching letter pairs.

Algorithm

1. Initialize an empty list pairs.
2. Iterate over the range from 0 to length of the string – 1 and append the tuple of ith character and (i+1)th character to the pairs list.
3. Initialize an empty list consecutive_pairs.
4. Filter the pairs list by checking if both characters in the pair are alphabets and have a difference of 1 in their ASCII codes.
5. Return the consecutive_pairs list.

Python3




string = "geeksforgeeks is best fgr geeks"
 
pairs = [(string[i], string[i+1]) for i in range(len(string)-1)]
 
consecutive_pairs = filter(lambda pair: pair[0].isalpha() and pair[1].isalpha() and ord(pair[1]) - ord(pair[0]) == 1, pairs)
 
result = list(consecutive_pairs)
 
print("The Consecutive matching letter pairs : {}".format(result))


Output

The Consecutive matching letter pairs : [('s', 't'), ('f', 'g')]

Time Complexity: O(n), where n is the length of the given string. The for loop that iterates over the range from 0 to length of the string – 1 takes O(n) time. The filter function takes O(n) time as it iterates over each pair in the pairs list.

Auxiliary Space: O(n), where n is the length of the given string. The pairs list takes O(n) space as it stores all pairs of consecutive characters in the given string. The consecutive_pairs list takes O(k) space, where k is the number of consecutive matching letter pairs in the given string. However, k cannot be greater than n/2, which means the space complexity remains O(n).



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads