Examples :
Input: geeks123available Output: (’available’, 123) Input: 98apple658pine Output: (’apple’, 658)
Approach # 1: brute force
This is naive or crude method to find the longest sequence of letters and numbers. We take two string variables of type longest_letter and longest_digit . We start the loop and check the consecutive substrings of letters and numbers. At each iteration, we check if the current alphabetic substring is greater than the longest alphabetic or numeric substring, respectively. If so, we assign the current letter and number substring to the longest letter and digit substring, respectively. Otherwise, do nothing.
while (i " len (s)):
|
Exit:
(’Geeksfor’,’ 123’)
Approach # 2: Using Python regular expressions
Python Regex is another method to solve this problem. Find a sequence of substrings of numbers and letters using a Python regex, and then find the longest substring length, respectively.
|
Output:
(’pythonengineering’,’ 123’)