Python | Find longest consecutive letter and digit substring
Given a String (may contain both letters and digits), write a Python program to find the longest consecutive letter and digit substring.
Examples:
Input : geeks123available
Output : ('available', 123)
Input : 98apple658pine
Output : ('apple', 658)
Approach #1 : Brute force
This is the Naive or brute force approach to find the longest consecutive letter and digit substring. We take two string type variable longest_letter and longest_digit. We start a loop and check for consecutive substrings of letter and digit. In each iteration, we check if the current letter substring is longer than the longest letter or digit substring respectively. If yes, we assign the current substring of letter and digit to the longest letter and digit substring respectively. Otherwise, do nothing.
# Python3 program to find the longest # consecutive substring of a certain type import re def longestSubstring(s): longest_letterSeq = '' longest_digitSeq = '' i = 0 while(i<len(s)): curr_letterSeq = '' curr_digitSeq = '' # For letter substring while(i<len(s) and s[i].isalpha()): curr_letterSeq += s[i] i+= 1 # For digit substring while(i<len(s) and s[i].isdigit()): curr_digitSeq += s[i] i+= 1 # Case handling if the character # is neither letter nor digit if(i< len(s) and not(s[i].isdigit()) and not(s[i].isalpha())) : i+= 1 if(len(curr_letterSeq) > len(longest_letterSeq) ): longest_letterSeq = curr_letterSeq if(len(curr_digitSeq) > len(longest_digitSeq) ): longest_digitSeq = curr_digitSeq return longest_letterSeq, longest_digitSeq # Driver Code str = '3Geeksfor123geeks3'print(longestSubstring(str)) |
('Geeksfor', '123')
Approach #2 : Using Python regex
Python Regex is another method to solve the given problem. Find the substring sequence of digits and letters using Python regex and then find the longest substring length respectively.
# Python3 program to find the longest # consecutive substring of a certain type import re def longestSubstring(str): letter = max(re.findall(r'\D+', str), key = len) digit = max(re.findall(r'\d+', str), key = len) return letter, digit # Driver Code str = 'geeks123geeksforgeeks1'print(longestSubstring(str)) |
('geeksforgeeks', '123')
Recommended Posts:
- SequenceMatcher in Python for Longest Common Substring
- Python | Find the list elements starting with specific letter
- Python Map | Length of the Longest Consecutive 1's in Binary Representation of a given integer
- Python regex to find sequences of one upper case letter followed by lower case letters
- Python | Find last occurrence of substring
- Python | Ways to find nth occurrence of substring in a string
- Python | Find k longest words in given list
- Python | Ways to check if given string contains only letter
- Python | Remove the given substring from end of string
- Python | All occurrences of substring in string
- Python | Get the string after occurrence of given substring
- Python | Frequency of substring in given string
- Python | Substring Key match in dictionary
- Python | Add substring at specific index
- Python | Duplicate substring removal from list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



