Python Regex to extract maximum numeric value from a string
Given an alphanumeric string, extract maximum numeric value from that string. Alphabets will only be in lower case. Examples:
Input : 100klh564abc365bg Output : 564 Maximum numeric value among 100, 564 and 365 is 564. Input : abchsd0sdhs Output : 0
This problem has existing solution please refer Extract maximum numeric value from a given string | Set 1 (General approach) link. We will solve this problem quickly in python using Regex. Approach is very simple,
- Find list of all integer numbers in string separated by lower case characters using re.findall(expression,string) method.
- Convert each number in form of string into decimal number and then find max of it.
Time complexity : O(n), where n is the length of the input string. This is because the function iterates through the input string once to find all the numbers using the regular expression, and then iterates through the numbers again to convert them to integers.
Space complexity : O(n)
My Personal Notes arrow_drop_up
Please Login to comment...