Given a alphanumeric string S, extract maximum numeric value from S.
Example 1:
Input:
S = 100klh564abc365bg
Output: 564
Explanation: Maximum numeric value 
among 100, 564 and 365 is 564.
Example 2:
Input:
S = abcdefg
Output: -1
Explanation: Return -1 if no numeric 
value is present. 
Your Task:  
You dont need to read input or print anything. Complete the function extractMaximum() which takes the string S as input parameters and returns the maximum numeric value. If it is not present in S, return -1.
 
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ |S| ≤ 10^{4}
-1 ≤ output value ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Mathematics

Starter code: #User function Template for python3

class Solution:
    def extractMaximum(self,S): 
        # code here

Solutions:
class Solution:

	def extractMaximum(self, line):
		(maxi, num) = (-1, 0)
		for symbol in line:
			if symbol.isdigit():
				num = num * 10 + int(symbol)
				if num > maxi:
					maxi = num
			else:
				num = 0
		return maxi


class Solution:

	def extractMaximum(self, S):
		import re
		numbers = re.findall('\\d+', S)
		if not numbers:
			return -1
		maximum = float('-inf')
		for num in numbers:
			maximum = max(maximum, int(num))
		return maximum


class Solution:

	def extractMaximum(self, S):
		import re
		x = re.findall('\\d+', S)
		x = list(map(int, x))
		if len(x) == 0:
			return -1
		return max(x)


import re

class Solution:

	def extractMaximum(self, s):
		t = re.split('[a-zA-Z]+', s)
		for i in range(len(t)):
			if t[i] == '':
				t[i] = -1
			else:
				t[i] = int(t[i])
		return max(t)


class Solution:

	def extractMaximum(self, S):
		l = ''
		l2 = []
		l3 = []
		for i in S:
			if i.isdigit():
				l += i
			else:
				l += ','
		z = l.split(',')
		for i in z:
			if i.isdigit():
				l2.append(i)
		for i in l2:
			l3.append(int(i))
		if l3 == []:
			return -1
		else:
			return max(l3)


class Solution:

	def extractMaximum(self, S):
		if S.isalpha():
			return -1
		else:
			l = []
			for i in S:
				if i.isalpha():
					S = S.replace(i, ' ')
			S = S.split()
			for i in S:
				l.append(int(i))
			return max(l)


class Solution:

	def extractMaximum(self, S):
		i = 0
		maxEle = -1
		while i < len(S):
			if S[i].isdigit():
				j = i + 1
				while j < len(S) and S[j].isdigit():
					j += 1
				maxEle = max(maxEle, int(S[i:j]))
				i = j
			else:
				i += 1
		return maxEle


class Solution:

	def extractMaximum(self, s):
		for i in s:
			if i.isalpha():
				s = s.replace(i, ' ')
		ans = s.split()
		if len(ans) == 0:
			return -1
		res = []
		for j in ans:
			res.append(int(j))
		return max(res)


import re

class Solution:

	def extractMaximum(self, S):
		pattern = re.compile('\\d+')
		num = re.findall(pattern, S)
		if len(num) != 0:
			lst = []
			for i in num:
				if i not in lst:
					lst.append(int(i))
			largest_Num = lst[0]
			for i in lst:
				if i > largest_Num:
					largest_Num = i
			return largest_Num
		else:
			return -1


class Solution:

	def extractMaximum(self, S):
		m = -1
		for i in range(len(S)):
			a = ''
			while ord(S[i]) >= 48 and ord(S[i]) <= 57:
				a += S[i]
				i += 1
				if i == len(S):
					break
			if len(a) != 0:
				m = max(m, int(a))
		return m


class Solution:

	def extractMaximum(self, S):
		maxVal = -1
		repeatVal = ''
		for i in S:
			if ord(i) >= 48 and ord(i) <= 57:
				repeatVal += i
				maxVal = max(maxVal, int(repeatVal))
			else:
				repeatVal = ''
		return maxVal


import re

class Solution:

	def extractMaximum(self, S):
		l = re.findall('\\d+', S)
		l = [int(x) for x in l]
		if len(l) != 0:
			return max(l)
		return -1


import re

class Solution:

	def extractMaximum(self, S):
		l = re.findall('[0-9]+', S)
		max = -1
		for i in l:
			if int(i) > max:
				max = int(i)
		return max


class Solution:

	def extractMaximum(self, S):
		for ch in S:
			if ch.isalpha():
				S = S.replace(ch, ' ')
		ans = S.split()
		if len(ans) == 0:
			return -1
		res = []
		for i in ans:
			res.append(int(i))
		return max(res)


import re

class Solution:

	def extractMaximum(self, S):
		mac = re.findall('\\d+', S)
		for i in range(len(mac)):
			mac[i] = int(mac[i])
		if len(mac) >= 1:
			return max(mac)
		else:
			return -1


class Solution:

	def extractMaximum(self, S):
		max_num = -1
		num = 0
		for i in S:
			if i.isdigit():
				num = num * 10 + (ord(i) - ord('0'))
				max_num = max(max_num, num)
			else:
				num = 0
		return max_num


import re

class Solution:

	def extractMaximum(self, S):
		num = re.findall('\\d+', S)
		if len(num) != 0:
			num = [int(x) for x in num]
			num = max(num, key=int)
			return num
		else:
			return -1


class Solution:

	def extractMaximum(self, S):
		i = 0
		m = -1
		while i < len(S):
			temp = ''
			while i < len(S) and S[i].isdigit():
				temp += S[i]
				i += 1
			i += 1
			if temp != '':
				m = max(m, int(temp))
		return m


import math

class Solution:

	def extractMaximum(self, S):
		temp = '0'
		c = 0
		max1 = -math.inf
		for i in S:
			if i.isdigit():
				temp += i
			else:
				c += 1
				max1 = max(max1, int(temp))
				temp = '0'
		if c == len(S):
			return -1
		return max(max1, int(temp))


class Solution:

	def extractMaximum(self, S):
		nums = {-1}
		s = ''
		for c in S:
			if c in '0123456789':
				s += c
			else:
				nums.add(int(s or -1))
				s = ''
		nums.add(int(s or -1))
		return max(nums)


import re

class Solution:

	def extractMaximum(self, S):
		nums = re.findall('\\d+', S)
		for num in range(len(nums)):
			nums[num] = int(nums[num])
		if len(nums) >= 1:
			return max(nums)
		else:
			return -1


class Solution:

	def extractMaximum(self, S):
		res = ''
		m = 0
		k = False
		for i in range(len(S)):
			if S[i].isnumeric():
				res += S[i]
				k = True
				if i + 1 == len(S) or not S[i + 1].isnumeric():
					res = int(res)
					m = max(m, res)
					res = ''
		if k == False:
			return -1
		else:
			return m


class Solution:

	def extractMaximum(self, s):
		ans = -1
		temp = ''
		for i in s:
			if i.isdigit():
				temp += i
				ans = max(ans, int(temp))
			else:
				temp = ''
		return ans


import re

class Solution:

	def extractMaximum(self, S):
		c = 0
		l = []
		S += '_'
		for i in range(len(S)):
			if S[i].isnumeric():
				c += 1
			else:
				if c != 0:
					l.append(int(S[i - c:i]))
				c = 0
		if len(l) == 0:
			return -1
		else:
			return max(l)


class Solution:

	def extractMaximum(self, S):
		nums = [-1]
		s = ''
		for c in S:
			if c >= '0' and c <= '9':
				s += c
			elif s != '':
				nums.append(int(s))
				s = ''
		if s != '':
			nums.append(int(s))
			s = ''
		return max(nums)


class Solution:

	def extractMaximum(self, S):
		nums = [-1]
		s = ''
		for c in S:
			if c in '0123456789':
				s += c
			elif s != '':
				nums.append(int(s))
				s = ''
		if s != '':
			nums.append(int(s))
			s = ''
		return max(nums)


class Solution:

	def extractMaximum(self, S):
		num = ''
		num_list = []
		max_val = -1
		for i in S:
			if i.isdigit():
				num = num + i
				max_val = max(int(num), max_val)
			elif i.isalpha():
				if num != '':
					max_val = max(int(num), max_val)
					num = ''
		if max_val != -1:
			return max_val
		else:
			return -1


class Solution:

	def extractMaximum(self, S, st='', c=0):
		for i in S:
			if i.isdigit():
				st += i
				c = 1
			else:
				st += ' '
		if c == 1:
			li = [int(i) for i in st.split()]
			return max(li)
		else:
			return -1


class Solution:

	def extractMaximum(self, S):
		import re
		x = re.findall('\\d+', S)
		w = []
		for i in range(len(x)):
			w.append(int(x[i]))
		if w == []:
			return -1
		else:
			return max(w)


class Solution:

	def extractMaximum(self, S):
		k = ''
		lis = []
		ans = -1
		for i in S:
			if i.isdigit():
				k += i
			else:
				if k != '':
					ans = max(ans, int(k))
				k = ''
		if k != '':
			ans = max(ans, int(k))
		return ans


import re

class Solution:

	def extractMaximum(self, S):
		l1 = re.findall('[0-9]+', S)
		max = 0
		for i in range(len(l1)):
			if max < int(l1[i]):
				max = int(l1[i])
		if len(l1) == 0:
			return -1
		else:
			return max


class Solution:

	def extractMaximum(self, S):
		ans = ''
		num_list = []
		for num in S:
			if num.isdigit():
				ans = ans + num
			elif len(ans) != 0:
				num_list.append(int(ans))
				ans = ''
		if len(ans) != 0:
			num_list.append(int(ans))
		if num_list:
			x = max(num_list)
			return x
		else:
			return -1


class Solution:

	def extractMaximum(self, S):
		b = ''
		l = []
		if S.isdigit():
			return S
		for i in range(len(S)):
			if S[i].isdigit():
				b += S[i]
				l.append(b)
			else:
				b = ''
		l = list(map(int, l))
		if len(l) != 0:
			return max(l)
		else:
			return -1


class Solution:

	def extractMaximum(self, s):
		p = []
		for i in range(len(s)):
			if s[i].isdigit():
				n = 0
				x = i
				while x < len(s) and s[x].isdigit():
					n = n * 10 + int(s[x])
					x += 1
				p.append(n)
			else:
				pass
		if len(p) == 0:
			return -1
		return max(p)


class Solution:

	def extractMaximum(self, S):
		str1 = ''
		for i in range(len(S)):
			if S[i].isalpha():
				str1 += ' '
			else:
				str1 += S[i]
		k = str1.split()
		l1 = list(map(int, k))
		if len(k) == 0:
			return -1
		else:
			return max(l1)


class Solution:

	def extractMaximum(self, S):
		max_num = -1
		current_num = ''
		for c in S:
			if c.isdigit():
				current_num += c
			elif current_num:
				max_num = max(max_num, int(current_num))
				current_num = ''
		if current_num:
			max_num = max(max_num, int(current_num))
		return max_num


class Solution:

	def extractMaximum(self, S):
		l = ''
		for i in S:
			if i.isdigit():
				l = l + i
			else:
				l = l + ' '
		b = l.split()
		c = []
		for i in b:
			c.append(int(i))
		if len(b) == 0:
			return -1
		return max(c)


class Solution:

	def extractMaximum(self, S):
		list_num = []
		num_str = ''
		for i in S:
			if i.isnumeric():
				num_str += i
			elif len(num_str) > 0:
				list_num.append(int(num_str))
				num_str = ''
		if len(num_str) > 0:
			list_num.append(int(num_str))
		if len(list_num) == 0:
			return -1
		return max(list_num)


class Solution:

	def extractMaximum(self, S):
		a = 0
		c = 0
		maxi = 0
		m = 0
		for i in S:
			if i.isnumeric():
				m = m * 10 + int(i)
				a = 1
			else:
				maxi = max(maxi, m)
				m = 0
		if a == 0:
			return -1
		else:
			return max(maxi, m)


class Solution:

	def extractMaximum(self, S):
		res = ''
		for i in S:
			if i.isdigit():
				res += i
			else:
				res += ' '
		res = res.split()
		res = [int(i) for i in res]
		if len(res) == 0:
			return -1
		else:
			return max(res)


class Solution:

	def extractMaximum(self, S):
		current_num = ''
		max_num = ''

		def updateMaxNum():
			nonlocal current_num, max_num
			if current_num != '':
				if max_num == '' or int(current_num) > int(max_num):
					max_num = current_num
				current_num = ''
		for i in range(len(S)):
			if not S[i].isdigit():
				updateMaxNum()
			else:
				current_num += S[i]
				if i == len(S) - 1:
					updateMaxNum()
		return int(max_num) if max_num != '' else -1


class Solution:

	def extractMaximum(self, s):
		p = []
		n = '1234567890'
		px = ''
		for i in s:
			if i in n:
				px += i
			else:
				if len(px) > 0:
					z = int(px)
					p.append(z)
				px = ''
		if px != '':
			z = int(px)
			p.append(z)
		if len(p) > 0:
			return max(p)
		return -1


class Solution:

	def extractMaximum(self, S):
		new_string = ''
		for i in S:
			if i.isdigit():
				new_string += i
			else:
				new_string += ' '
		list_of_values = new_string.split()
		try:
			list_of_values = [int(i) for i in list_of_values]
			return max(list_of_values)
		except:
			return -1


class Solution:

	def extractMaximum(self, S):
		mul = 0
		maximum = 0
		k = 0
		for i in S:
			if i >= '0' and i <= '9':
				mul = mul * 10 + int(i)
				k += 1
			else:
				maximum = max(maximum, mul)
				mul = 0
		if k == 0:
			return -1
		x = max(maximum, mul)
		return x


import re

class Solution:

	def extractMaximum(self, S):
		if S.isalpha():
			return -1
		n = re.findall('\\d+', S)
		b = map(int, n)
		return max(b)


import re

class Solution:

	def extractMaximum(self, S):
		a = re.findall('\\d+', S)
		li = []
		for i in a:
			li.append(int(i))
		if len(a) == 0:
			return -1
		return max(li)


import re

class Solution:

	def extractMaximum(self, S):
		lst = re.findall('\\d+', S)
		if lst != []:
			return max(list(map(int, re.findall('\\d+', S))))
		else:
			return -1


class Solution:

	def extractMaximum(self, S):
		nums = []
		cur_num = ''
		i = 0
		while i < len(S):
			if S[i].isdigit():
				cur_num += S[i]
			else:
				if cur_num != '':
					nums.append(int(cur_num))
				cur_num = ''
			i += 1
		if cur_num != '':
			nums.append(int(cur_num))
		if len(nums):
			return max(nums)
		return -1


class Solution:

	def extractMaximum(self, S):
		a = 'abcdefghijklmnopqrstuvwxyz'
		s1 = set(list(S))
		v = []
		c = 0
		for i in range(len(S)):
			if S[i] in a:
				c = c + 1
				S = S.replace(S[i], ' ')
		if c != len(s1):
			l = S.split()
			for i in range(len(l)):
				l[i] = int(l[i])
			return max(l)
		else:
			return -1


class Solution:

	def extractMaximum(self, S):
		max_num = None
		num_str = ''
		for c in S:
			if c.isdigit() or c == '-':
				num_str += c
			elif num_str:
				num = int(num_str)
				if max_num is None or num > max_num:
					max_num = num
				num_str = ''
		if num_str:
			num = int(num_str)
			if max_num is None or num > max_num:
				max_num = num
		if max_num is None:
			return -1
		else:
			return max_num


class Solution:

	def extractMaximum(self, S):
		S += 'A'
		s = 0
		a = []
		c = 0
		for i in S:
			if i.isnumeric():
				c += 1
				s = s * 10 + int(i)
			else:
				a.append(s)
				s = 0
		if max(a) == 0 and c == 0:
			return -1
		else:
			return max(a)


import re

class Solution:

	def extractMaximum(self, S):
		numeric = re.finditer('\\d+', S)
		values = [int(match.group()) for match in numeric]
		if len(values) > 0:
			return max(values)
		else:
			return -1


class Solution:

	def extractMaximum(self, S):
		l = []
		n = ''
		n1 = ''
		for i in S:
			if i.isdigit():
				fg = True
				n = n + i
				n1 = n
			else:
				fg = False
				if n != '':
					l.append(int(n))
					n = ''
		if n1 and fg:
			l.append(int(n1))
		if len(l) == 0:
			return -1
		l.sort()
		return l[-1]


class Solution:

	def extractMaximum(self, S):
		s = list(S)
		numb = set(('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'))
		i = 0
		mx = -1
		while i < len(S):
			j = 0
			num = ''
			while s[i + j] in numb:
				num += s[i + j]
				if i + j + 1 < len(S):
					j += 1
				else:
					break
			if len(num) != 0:
				num = int(num)
				if mx < num:
					mx = num
			i += j + 1
		if mx != -1:
			return mx
		else:
			return -1


class Solution:

	def extractMaximum(self, S):
		is_previous_char_num = False
		num_list = []
		num = ''
		for i in range(len(S)):
			char = S[i]
			if char.isdigit():
				num = num + char
				if i + 1 < len(S):
					next_char = S[i + 1]
					if not next_char.isdigit():
						num_list.append(int(num))
						num = ''
				elif i == len(S) - 1:
					num_list.append(int(num))
		if num_list:
			return max(num_list)
		else:
			return -1


class Solution:

	def extractMaximum(self, S):
		numList = []
		numStr = ''
		for i in S:
			if i.isdigit():
				numStr += i
			else:
				numStr += ':'
		numList = numStr.split(':')
		output = []
		for i in numList:
			if len(i) > 0:
				output.append(int(i))
		return max(output) if output else -1


class Solution:

	def extractMaximum(self, S):
		S = S + ' '
		x = '1234567890'
		y = ''
		z = []
		for i in range(len(S)):
			if S[i] in x:
				y = y + S[i]
			elif y != '':
				z.append(int(y))
				y = ''
		z.sort()
		if z != []:
			return z[-1]
		else:
			return -1


class Solution:

	def extractMaximum(self, S):
		max_num = '-1'
		k = 0
		while k < len(S):
			j = k
			num = ''
			while j < len(S) and 48 <= ord(S[j]) < 58:
				if S[j] == '0' and max_num == '-1':
					max_num = '0'
				if len(num) == 0 and S[j] == '0':
					j += 1
					continue
				num += S[j]
				j += 1
			k += j - k
			k += 1
			if num != '' and int(num) > int(max_num):
				max_num = num
		return int(max_num)


class Solution:

	def extractMaximum(self, S):
		max_num = '-1'
		for i in range(len(S)):
			j = i
			num = ''
			while j < len(S) and 48 <= ord(S[j]) < 58:
				if S[j] == '0' and max_num == '-1':
					max_num = '0'
				if len(num) == 0 and S[j] == '0':
					j += 1
					continue
				num += S[j]
				j += 1
			if num != '' and int(num) > int(max_num):
				max_num = num
		return int(max_num)


class Solution:

	def extractMaximum(self, s):
		l = []
		i = 0
		while i < len(s):
			temp = ''
			if s[i].isdigit():
				while i < len(s) and s[i].isdigit():
					temp += s[i]
					i += 1
				l.append(int(temp))
			else:
				i += 1
		return max(l) if len(l) > 0 else -1


import re

class Solution:

	def extractMaximum(self, S):
		a = re.findall('\\d+', S)
		if not a:
			return -1
		else:
			b = max([int(i) for i in a])
			return b


class Solution:

	def extractMaximum(self, S):
		maxi = float('-inf')
		if S.isalpha():
			return -1
		if S.isdigit():
			return int(S)
		s1 = ''
		for i in range(len(S)):
			if S[i].isdigit():
				s1 += S[i]
				if int(s1) > maxi:
					maxi = int(s1)
			else:
				s1 = ''
		return maxi


class Solution:

	def extractMaximum(self, S):
		s1 = ''
		mx = 0
		for i in S:
			if i.isdigit():
				s1 += i
				if int(s1) > mx:
					mx = int(s1)
			else:
				s1 = ''
		if S.isalpha():
			return -1
		else:
			return mx


class Solution:

	def extractMaximum(self, S):
		a = []
		s = ''
		for i in S:
			if i.isdigit():
				s += i
			elif not s:
				continue
			else:
				a.append(int(s))
				s = ''
		if s:
			a.append(int(s))
		if not a:
			return -1
		else:
			return max(a)


class Solution:

	def extractMaximum(self, S):
		temp = ''
		num = []
		for e in S:
			if e.isdigit():
				temp += e
			elif temp != '':
				num.append(int(temp))
				temp = ''
		else:
			if len(num) == 0 and temp == '':
				return -1
			elif temp != '':
				num.append(int(temp))
		return max(num)


class Solution:

	def extractMaximum(self, S):
		nums = []
		numeric = ''
		for i in S:
			if i.isdigit():
				numeric += i
			elif len(numeric) > 0:
				nums.append(int(numeric))
				numeric = ''
		if len(numeric) > 0:
			nums.append(int(numeric))
		if len(nums) > 0:
			return max(nums)
		else:
			return -1


class Solution:

	def extractMaximum(self, S):
		l = ''
		for i in S:
			if i.isdigit():
				l += i
			else:
				l += ' '
		p = l.split()
		for i in p:
			if i.isspace():
				p.remove(i)
		if len(p) != 0:
			p = [int(x) for x in p]
			return max(p)
		else:
			return -1


import re

class Solution:

	def extractMaximum(self, S):
		ans = [int(i) for i in re.findall('\\d+', S)]
		return max(ans) if len(ans) > 0 else -1


class Solution:

	def extractMaximum(self, s):
		a = []
		r = ''
		for i in range(0, len(s)):
			if s[i] >= '0' and s[i] <= '9':
				r += s[i]
			elif r != '':
				a.append(int(r))
				r = ''
		if r != '':
			a.append(int(r))
		a.sort()
		if len(a) == 0:
			return -1
		return a[-1]