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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course