Change language

Python | Find the longest sequence of letters and numbers

| |

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.

# Python3 program to find the longest
# sequential substring of a specific type

import re

 

def longestSubstring (s):

 

longest_letterSeq = ’’

longest_digitSeq = ’’

i = 0

< p> while (i " len (s)):

 

curr_letterSeq = ’’

curr_digitSeq = ’’

 

# For literal substring

while (i " len (s) and s [i] .isalpha ()):

  curr_letterS eq + = s [i]

i + = 1

 

# For numeric substring

while (i " len (s) and s [i] .isdigit ()):

curr_digitSeq + = s [i]

  i + = 1

  

  Handle case if character

# is neither a letter nor digit

if (i & lt;  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 ))

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.

# Python3 program to find the longest
# sequential 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 = ’geeks123pythonengineering1’

print (longestSubstring ( str ))

Output:

 (’pythonengineering’,’ 123’) 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method