Extract maximum numeric value from a given string | Set 1 ( General approach ) | Utility function to find maximum string ; If both having equal lengths ; Reach first unmatched character / value ; Return string with maximum value ; If different lengths return string with maximum length ; Function to extract the maximum value ; Start traversing the string ; Ignore leading zeroes ; Store numeric value into a string ; Update maximum string ; To handle the case if there is only 0 numeric value ; Return maximum string ; Driver Code
def maximumNum ( curr_num , res ) : NEW_LINE INDENT len1 = len ( curr_num ) ; NEW_LINE len2 = len ( res ) ; NEW_LINE if ( len1 == len2 ) : NEW_LINE INDENT i = 0 ; NEW_LINE while ( curr_num [ i ] == res [ i ] ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT if ( curr_num [ i ] < res [ i ] ) : NEW_LINE INDENT return res ; NEW_LINE DEDENT else : NEW_LINE INDENT return curr_num ; NEW_LINE DEDENT DEDENT return res if ( len1 < len2 ) else curr_num ; NEW_LINE DEDENT def extractMaximum ( str ) : NEW_LINE INDENT n = len ( str ) ; NEW_LINE curr_num = " " ; NEW_LINE res = " " ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( i < n and str [ i ] == '0' ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT while ( i < n and str [ i ] >= '0' and str [ i ] <= '9' ) : NEW_LINE INDENT curr_num += str [ i ] ; NEW_LINE i += 1 ; NEW_LINE DEDENT if ( i == n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT if ( len ( curr_num ) > 0 ) : NEW_LINE INDENT i -= 1 ; NEW_LINE DEDENT res = maximumNum ( curr_num , res ) ; NEW_LINE curr_num = " " ; NEW_LINE DEDENT if ( len ( curr_num ) == 0 and len ( res ) == 0 ) : NEW_LINE INDENT res += '0' ; NEW_LINE DEDENT return maximumNum ( curr_num , res ) ; NEW_LINE DEDENT str = "100klh564abc365bg " ; NEW_LINE print ( extractMaximum ( str ) ) ; NEW_LINE