Given a sentence containing several words and numbers. Find the largest number among them which does not contain 9. 
Example 1:
Input:
Sentence="This is alpha 5057 and 97"
Output:
5057
Explanation:
5057 is the only number that does
not have a 9.
Example 2:
Input:
Sentence="Another input 9007"
Output:
-1
Explanation:
Since there is no number that
does not contain a 9,output is -1.
Your task:
You don't need to read input or print anything. Your task is to complete the function ExtractNumber() which takes a string S as input parameter and returns the largest number that does not contain a 9. If no such number exists, return -1.
Expected Time Complexity:O(N)
Expected Auxillary Space:O(1)
Constraints:
1<=Length of Sentence<=10^{6}
1<=answer<=10^{18}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

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

Solutions:
class Solution:

	def ExtractNumber(self, S):
		l = -1
		S = list(S.split(' '))
		for i in range(len(S)):
			if S[i].isdigit() == True:
				if '9' in S[i]:
					continue
				elif l < int(S[i]):
					l = int(S[i])
		return l


class Solution:

	def ExtractNumber(self, S):
		l = S.split()
		r = -1
		for i in l:
			if i.isdigit() and '9' not in i:
				n = int(i)
				if r == -1 or n > r:
					r = n
		return r


class Solution:

	def ExtractNumber(self, S):
		b = S.split(' ')
		max = -1
		for i in b:
			if i.isdigit() == True and max < int(i):
				if '9' not in i:
					max = int(i)
		if max != -1:
			return max
		else:
			return -1


class Solution:

	def ExtractNumber(self, S):
		str = S.split(' ')
		nums = []
		for i in str:
			if i.isnumeric():
				if '9' not in i:
					nums.append(i)
		curr = -1
		for i in nums:
			curr = max(curr, int(i))
		return curr


class Solution:

	def ExtractNumber(self, S):
		S = list(S.split(' '))
		l = []
		for i in range(len(S)):
			if S[i].isdigit() == True:
				if '9' not in S[i]:
					l.append(int(S[i]))
		if len(l) == 0:
			return -1
		else:
			return max(l)


class Solution:

	def ExtractNumber(self, S):
		listing = S.split()
		min = -1
		for i in listing:
			if i.isnumeric():
				if '9' in i:
					continue
				elif min < int(i):
					min = int(i)
		return min


class Solution:

	def ExtractNumber(self, S):
		res = [-1]
		arr = S.split()
		for i in arr:
			if i.isdigit() and i.count('9') == 0:
				res.append(int(i))
		return max(res)


class Solution:

	def ExtractNumber(self, S):
		words = S.split()
		max = 0
		for word in words:
			if word.isdigit():
				if int(word) > max and '9' not in word:
					max = int(word)
		return max if max != 0 else -1


class Solution:

	def ExtractNumber(self, S):
		self.S = S
		lst = self.S.split(' ')
		max = 0
		for i in lst:
			if i.isdigit():
				if '9' in i:
					pass
				elif max < int(i):
					max = int(i)
		if max == 0:
			return -1
		else:
			return max


class Solution:

	def ExtractNumber(self, S):
		l = []
		for i in S.split(' '):
			if i.isnumeric() and '9' not in i:
				l.append(int(i))
		if len(l) == 0:
			return -1
		return max(l)


class Solution:

	def ExtractNumber(self, S):
		s = list(S.split())
		lst = []
		for i in s:
			try:
				i = int(i)
				if '9' not in str(i):
					lst.append(i)
			except ValueError:
				pass
		if lst == []:
			return -1
		else:
			return max(lst)


class Solution:

	def ExtractNumber(self, S):
		arr = S.split()
		arr2 = list(filter(lambda x: x.isdigit(), arr))
		res = -1
		for i in arr2:
			if '9' in i:
				continue
			else:
				res = max(int(i), res)
		return int(res)


import re

class Solution:

	def ExtractNumber(self, S):
		res = re.findall('\\d+', S)
		max = 0
		for i in res:
			if '9' not in i and int(i) > max:
				max = int(i)
		if max == 0:
			return -1
		else:
			return max


class Solution:

	def ExtractNumber(self, s):
		l = []
		s1 = ''
		for i in s:
			if i >= '0' and i <= '9':
				s1 += i
			elif s1 != '':
				l.append(s1)
				s1 = ''
		if s1 != '':
			l.append(s1)
		l1 = []
		for i in l:
			if '9' not in i:
				l1.append(int(i))
		if l1 == []:
			return -1
		return max(l1)


class Solution:

	def ExtractNumber(self, S):
		k = S.split()
		h = []
		for i in k:
			if i.isnumeric():
				h.append(int(i))
		h.sort()
		h.reverse()
		for i in h:
			g = str(i)
			if '9' not in g:
				return int(g)
		return -1


import re

class Solution:

	def ExtractNumber(self, S):
		m = re.findall('\\d+', S)
		q = []
		for i in m:
			if '9' not in i:
				q.append(i)
		if q:
			return max([int(i) for i in q])
		return -1


class Solution:

	def ExtractNumber(self, S):
		l = [i for i in S.split() if i.isdigit()]
		l1 = []
		for i in l:
			if '9' not in i:
				ll = int(i)
				l1.append(ll)
		if l1 == []:
			return -1
		else:
			return max(l1)


class Solution:

	def ExtractNumber(self, s):
		l = s.split(' ')
		maxi = -1
		for i in l:
			if '9' not in i and i.isdigit():
				maxi = max(int(i), maxi)
		return maxi


class Solution:

	def ExtractNumber(self, S):
		arr = S.split(' ')
		nums = []
		for item in arr:
			if item.isnumeric():
				if item.find('9') == -1:
					nums.append(item)
		curr_max = -1
		for item in nums:
			curr_max = max(curr_max, int(item))
		return curr_max


class Solution:

	def ExtractNumber(self, s):
		import re
		r = re.findall('[0-9]+', s)
		m = 0
		for i in r:
			if int(i) > m and '9' not in i:
				m = int(i)
		if m == 0:
			return -1
		else:
			return m


import re

class Solution:

	def ExtractNumber(self, S):
		result = re.findall('(?im)[0-9]+', S)
		max = 0
		for i in result:
			if int(i) > max and '9' not in i:
				max = int(i)
		if max == 0:
			return -1
		else:
			return max


class Solution:

	def ExtractNumber(self, S):
		res = list()
		for i in S.split():
			if i.isdigit() and '9' not in i:
				res.append(int(i))
		return -1 if not res else max(res)


class Solution:

	def ExtractNumber(self, S):
		s = S.split()
		numbers = []
		for i in range(len(s)):
			if s[i].isnumeric():
				numbers.append(int(s[i]))
		result = []
		for i in range(len(numbers)):
			num = numbers[i]
			while num:
				d = num % 10
				if d == 9:
					break
				num = num // 10
			if num == 0:
				result.append(numbers[i])
		if len(result) == 0:
			return -1
		elif len(result) == 1:
			return result[0]
		else:
			return max(result)


class Solution:

	def ExtractNumber(self, S):
		ans = -1
		S = S.split()
		digit = '123456780'
		for i in S:
			for j in i:
				if j not in digit:
					break
			else:
				ans = max(ans, int(i))
		return ans


class Solution:

	def ExtractNumber(self, S):
		ls = S.split(' ')
		ans = -1
		for i in ls:
			if i.isnumeric():
				flag = True
				for j in i:
					if j == '9':
						flag = False
						break
				if flag == True:
					ans = max(ans, int(i))
		return ans


class Solution:

	def ExtractNumber(self, S):
		str_S = ''
		list_num = []
		for i in S:
			if i.isnumeric():
				str_S += i
			elif str_S != '':
				if '9' in str_S:
					str_S = ''
					continue
				list_num.append(int(str_S))
				str_S = ''
		if str_S != '' and '9' not in str_S:
			list_num.append(int(str_S))
		if len(list_num) == 0:
			return -1
		return max(list_num)


class Solution:

	def ExtractNumber(self, S):
		a = []
		l1 = S.split()
		for i in l1:
			if i.isdigit():
				if '9' not in i:
					a.append(int(i))
		if len(a) == 0:
			return -1
		return max(a)


class Solution:

	def ExtractNumber(self, S):
		S = S.split()
		v = []
		for i in S:
			if i.isdigit() and '9' not in i:
				x = int(i)
				v.append(x)
		if not v:
			return -1
		return max(v)


class Solution:

	def ExtractNumber(self, S):
		s = S.split(' ')
		max1 = -1
		for i in s:
			if i.isnumeric():
				if '9' not in i:
					max1 = max(max1, int(i))
		if max1 == -1:
			return -1
		return max1


class Solution:

	def ExtractNumber(self, S):
		S = S.split()
		lst = []
		for i in S:
			if i.isnumeric() and '9' not in i:
				lst.append(int(i))
		return max(lst) if lst else -1


class Solution:

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


class Solution:

	def ExtractNumber(self, S):
		ans = -1
		for s in S.split():
			if s.isdigit() and s.find('9') == -1:
				ans = max(ans, int(s))
		return ans


class Solution:

	def ExtractNumber(self, S):
		l = S.split()
		res = 0
		for i in l:
			try:
				x = int(i)
				if type(x) == int and i.count('9') == 0:
					res = max(res, x)
			except:
				continue
		if res == 0:
			return -1
		return res


import re

class Solution:

	def ExtractNumber(self, S):
		exp = '[0-9]+'
		nos = list(re.findall(exp, S))
		nos = [x for x in nos if '9' not in x]
		if not nos:
			return -1
		nos = [int(x) for x in nos]
		return max(nos)


class Solution:

	def ExtractNumber(self, str):
		li = []
		ans = []
		li = str.split(' ')
		for i in li:
			if i.isdigit() == True:
				if '9' not in i:
					ans.append(int(i))
		if len(ans) == 0:
			return '-1'
		return max(ans)


class Solution:

	def ExtractNumber(self, S):
		temp = '0123456789'
		t = []
		S = S.split()
		for i in S:
			if i[0] in temp:
				if '9' not in i:
					t.append(int(i))
		if len(t) == 0:
			return -1
		return max(t)


class Solution:

	def ExtractNumber(self, S):
		max_ = -1
		for st in S.split():
			if st.isdigit() and '9' not in st:
				max_ = max(max_, int(st))
		return max_


class Solution:

	def ExtractNumber(self, S):
		a = S.split(' ')
		p = []
		for i in a:
			try:
				i = int(i)
				i = str(i)
				if '9' not in i:
					p.append(int(i))
			except:
				pass
		if len(p) == 0:
			return -1
		return max(p)


import re

class Solution:

	def ExtractNumber(self, S):
		r = re.compile('\\d+')
		m = r.findall(S)
		ans = -1
		lens = -1
		if len(m) > 0:
			for j in range(len(m)):
				if '9' not in m[j] and lens < int(m[j]):
					ans = m[j]
					lens = int(ans)
		return ans


class Solution:

	def ExtractNumber(self, S):
		a = S.split(' ')
		l = []
		for i in a:
			if i.isdigit() and '9' not in i:
				l.append(int(i))
		if len(l) == 0:
			return -1
		else:
			return max(l)


class Solution:

	def ExtractNumber(self, S):
		li = list(S.split(' '))
		lis = []
		for i in li:
			if i.isnumeric() and '9' not in i:
				lis.append(int(i))
		if len(lis) > 0:
			return max(lis)
		else:
			return -1


class Solution:

	def ExtractNumber(self, S):
		f = S.split()
		o = []
		for i in f:
			if i.isdigit():
				if '9' not in i:
					o.append(int(i))
		if len(o) == 0:
			return -1
		else:
			return max(o)


import re

class Solution:

	def ExtractNumber(self, S):
		lst = re.findall('\\d+', S)
		lst = list(map(int, lst))
		lst.sort(reverse=1)
		ans = None
		for num in lst:
			if '9' in str(num):
				continue
			else:
				ans = num
				break
		else:
			ans = -1
		return ans


class Solution:

	def ExtractNumber(self, S):
		s = []
		i = 0
		c = ''
		S = S.split(' ')
		while i < len(S):
			if S[i].isnumeric():
				if '9' not in S[i]:
					s.append(int(S[i]))
			i += 1
		if len(s) == 0:
			return -1
		s.sort()
		return s[-1]


class Solution:

	def ExtractNumber(self, S):
		large = -1
		S = S.split(' ')
		res = []
		for i in range(len(S)):
			if S[i].isdigit():
				if '9' not in S[i]:
					res.append(int(S[i]))
		if len(res) == 0:
			return -1
		else:
			return max(res)


import re

class Solution:

	def ExtractNumber(self, s):
		x = re.findall('\\b[0-8]+\\b', s)
		res = list(map(int, x))
		if x:
			return max(res)
		return -1


class Solution:

	def ExtractNumber(self, S):
		m = -1
		for i in S.split():
			if i.isdigit() and '9' not in i:
				m = max(m, int(i))
		return m


class Solution:

	def ExtractNumber(self, S):
		mylist = [e for e in S.split() if e.isdigit() and '9' not in e]
		if len(mylist) == 0:
			return -1
		num_list = list(map(int, mylist))
		return max(num_list)


class Solution:

	def ExtractNumber(self, S):
		k = S.split()
		d = '012345678'
		c = []
		for i in k:
			for j in i:
				if j in d and '9' not in i:
					c.append(i)
		l = []
		for i in c:
			l.append(int(i))
		if len(l) >= 1:
			return max(l)
		return -1


class Solution:

	def ExtractNumber(self, S):
		l = S.strip().split(' ')
		maxi = 0
		for i in l:
			if i.isnumeric():
				if not i.__contains__('9'):
					if int(i) > maxi:
						maxi = int(i)
		if maxi:
			return maxi
		else:
			return -1


import re

class Solution:

	def ExtractNumber(self, S):
		l = []
		s = re.findall('\\b\\d+\\b', S)
		for num in s:
			if '9' not in num:
				l.append(int(num))
		if l:
			return max(l)
		else:
			return -1


class Solution:

	def ExtractNumber(self, S):
		n = [int(i) for i in S.split() if i.isdigit() and '9' not in i]
		if len(n) == 0:
			k = -1
		else:
			k = max(n)
		return k