Find Longest Consecutive Letter and Digit Substring - Python
Last Updated :
12 Feb, 2025
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 letters and digits within a given string. For example, consider the string "3Geeksfor123geeks3". The longest consecutive letter substring would be "Geeksfor" and the longest consecutive digit substring would be "123".
Using re Module
re module provides a way to extract all contiguous letter and digit sequences from the string using regular expressions. We can use re.findall() to capture sequences of letters both uppercase and lowercase and digits and max() to find the longest sequence based on length.
Python
import re
s = '3Geeksfor123geeks3'
# Extract letter and digit sequences,then find the longest
a = max(re.findall(r'[a-zA-Z]+', s), key=len, default='')
b = max(re.findall(r'\d+', s), key=len, default='')
print(a, b)
Explanation:
re.findall(r'[a-zA-Z]+', s)
e
xtracts all contiguous sequences of letters both lowercase and uppercase from the string s
.re.findall(r'\d+', s)
e
xtracts all contiguous sequences of digits (0-9) from the string s
.
max(..., key=len, default='')
finds the longest sequence from each list based on the length of the sequences and if no sequences are found, it returns an empty string (''
).
itertools.groupby() groups consecutive characters in the string based on whether they are alphabetic or numeric. After grouping, we can iterate through the groups, updating the longest consecutive sequences for letters and digits.
Python
import itertools
s = "Geeksfor123"
a = '' # longest letter
b = '' # longest digit
c = '' # current letter
d = '' # current digit
for key, group in itertools.groupby(s, key=str.isalpha):
group_str = ''.join(group) # Join the characters in the group
if key: # If the group contains alphabetic characters
if len(group_str) > len(c):
c = group_str
if len(c) > len(a):
a = c
else: # If the group contains digits
if len(group_str) > len(d):
d = group_str
if len(d) > len(b):
b = d
print(a,b)
Explanation:
itertools.groupby(s, key=str.isalpha)
groups consecutive alphabetic and non-alphabetic characters. ''.join(group)
converts each group into a string, stored in group_str
.
- For Letters (key = True) if the group is alphabetic, update c with the longest letter sequence. If c is longer than a, update a.
- For Digits (key = False) if the group is numeric, update d with the longest digit sequence. If d is longer than b, update b.
Using sliding window approach
Sliding window technique processes the string in a single pass, maintaining variables to track the longest sequences of letters and digits. It skips over non-alphanumeric characters and updates the longest sequence dynamically.
Python
s = '3Geeksfor123geeks3'
a = '' # longest letter
b = '' # longest digit
i = 0 # index pointer
while i < len(s):
# Track letter sequence
if s[i].isalpha():
start = i # Mark the start index of the letter sequence
while i < len(s) and s[i].isalpha(): # Continue while characters are alphabetic
i += 1
# Update 'a' if the current letter sequence is longer
a = max(a, s[start:i], key=len)
# Track digit sequence
elif s[i].isdigit():
start = i # Mark the start index of the digit sequence
while i < len(s) and s[i].isdigit(): # Continue while characters are digits
i += 1
# Update 'b' if the current digit sequence is longer
b = max(b, s[start:i], key=len)
# Skip non-alphanumeric characters
else:
i += 1
print(a, b)
Explanation:
- First iteration first character is
'3'
, which is not a letter or digit so i
is incremented. - Letter sequence next characters form the letter sequence
'Geeksfor'
and a
is updated to 'Geeksfor'
. - Digit sequence next characters form the digit sequence
'123'
and b
is updated to '123'
. - Final result longest letter sequence is
'Geeksfor'
and the longest digit sequence is '123'
.
Using lambda Expressions
Lambda functions, combined with re.findall(), allow a concise approach to extract and find the longest letter and digit sequences by comparing their lengths. This method is efficient and provides a quick solution.
Python
import re
res = lambda s: (
max(re.findall('[a-zA-Z]+', s), key=len), # Find all letter sequences and return the longest
max(re.findall('\d+', s), key=len) # Find all digit sequences and return the longest
)
s = '3Geeksfor123geeks3'
print(res(s))
Output('Geeksfor', '123')
Explanation:
re.findall(r'[a-zA-Z]+', s)
extracts all letter sequences from the string s
.re.findall(r'\d+', s)
extracts all digit sequences from the string s
.max(..., key=len, default='')
finds the longest sequence by comparing lengths and returning an empty string if no sequences are found.
Similar Reads
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
First N letters String Construction - Python The task of constructing a string from the first N letters of a given string in Python involves extracting a specific portion of the string, starting from the beginning and including the first N characters. For example, if we have the string "GeeksForGeeks" and want to extract the first 5 characters
3 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 program to calculate the number of digits and letters in a string In this article, we will check various methods to calculate the number of digits and letters in a string. Using a for loop to remove empty strings from a list involves iterating through the list, checking if each string is not empty, and adding it to a new list.Pythons = "Hello123!" # Initialize cou
3 min read
Python | Count overlapping substring in a given string Given a string and a sub-string, the task is to get the count of overlapping substring from the given string. Note that in Python, the count() function returns the number of substrings in a given string, but it does not give correct results when two occurrences of the substring overlap. Consider thi
2 min read