Given a String (may contain both letters and digits), write a Python program to find the longest consecutive letter and digit substring.
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.
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.
- 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 | All occurrences of substring in string
- Python | Substring Key match in dictionary
- Python | Frequency of substring in given string
- Python | Get the string after occurrence of given substring
- Python | Remove the given substring from end of string
- 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 firstname.lastname@example.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.