Given a string str containing alphanumeric characters. The task is to calculate the sum of all the numbers present in the string.
Example 1:
Input:
str = 1abc23
Output: 24
Explanation: 1 and 23 are numbers in the
string which is added to get the sum as
24.
Example 2:
Input:
str = geeks4geeks
Output: 4
Explanation: 4 is the only number, so the
sum is 4.
Your Task:
The task is to complete the function findSum() which finds and returns the sum of numbers in the string.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 <= length of the string <= 10^{5}
Sum of Numbers <= 10^{5}

Difficulty: EASY
Tags: String algorithms

Data structures
Skill types: Data structures
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Starter code:

#User function Template for python3

class Solution:
    
    #Function to calculate sum of all numbers present in a string.
    def findSum(self,s):
        #code here


Solutions:
class Solution:

	def findSum(self, s):
		temp = 0
		ans = 0
		for item in s:
			if item.isdigit():
				if temp == 0:
					temp = int(item)
				else:
					temp = 10 * temp + int(item)
			else:
				ans += temp
				temp = 0
		ans += temp
		return ans


class Solution:

	def findSum(self, s):
		temp = []
		ans = 0
		for item in s:
			if item.isdigit():
				temp.append(item)
			else:
				if len(temp) > 0:
					ans += int(''.join(temp))
				temp = []
		if len(temp) > 0:
			ans += int(''.join(temp))
		return ans


class Solution:

	def findSum(self, s):
		temp = '0'
		add = 0
		for i in s:
			if i.isdigit():
				temp += i
			else:
				add += int(temp)
				temp = '0'
		return add + int(temp)


class Solution:

	def findSum(self, s):
		res = ''
		for char in s:
			if char.isalpha():
				res += ' '
			else:
				res += char
		check = res.split()
		sumanswer = 0
		for nums in check:
			number = int(nums)
			sumanswer += number
		return sumanswer


import string

class Solution:

	def findSum(self, str):
		s = ''
		add = 0
		for i in str:
			if i in string.ascii_letters:
				s += ','
			else:
				s += i
		for i in s.split(','):
			try:
				add += int(i)
			except:
				pass
		return add


class Solution:

	def findSum(self, s):
		k = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM'
		j = []
		b = ''
		for i in range(len(s)):
			if s[i] in k:
				if len(b) > 0:
					j.append(int(b))
					b = ''
			if s[i] not in k:
				b = b + s[i]
				if i == len(s) - 1:
					j.append(int(b))
		return sum(j)


class Solution:

	def findSum(self, s):
		c = 0
		i = 0
		while i < len(s):
			if s[i].isdigit():
				j = i
				while i + 1 < len(s) and s[i + 1].isdigit():
					i += 1
				c += int(s[j:i + 1])
			i += 1
		return c


class Solution:

	def findSum(self, s):
		sum = 0
		num = ''
		for ch in s:
			if ch.isdigit():
				num += ch
			elif num != '':
				sum += int(num)
				num = ''
		if num != '':
			sum += int(num)
		return sum


class Solution:

	def findSum(self, s):
		current_num = 0
		total_sum = 0
		for ch in s:
			if ch.isdigit():
				current_num = current_num * 10 + int(ch)
			else:
				total_sum += current_num
				current_num = 0
		total_sum += current_num
		return total_sum


class Solution:

	def findSum(self, s):
		num = 0
		sum = 0
		for i in s:
			if i.isdigit():
				num = num * 10 + int(i)
			else:
				sum += num
				num = 0
		return sum + num


class Solution:

	def findSum(self, s):
		count = ''
		sum = 0
		a = []
		for i in s:
			if i.isnumeric():
				count += i
			else:
				count += ','
		count_split = count.split(',')
		for i in count_split:
			if i.isdigit():
				a.append(i)
		for i in a:
			sum = sum + int(i)
		return sum


class Solution:

	def findSum(self, s):
		result = ''
		sum = 0
		for i in s:
			if i.isalpha():
				result += ' '
			else:
				result += i
		for i in result.split():
			a = int(i)
			sum += a
		return sum


class Solution:

	def findSum(self, s):
		t = ''
		for i in s:
			if i.isnumeric() == True:
				t += i
			else:
				t += ' '
		L = t.split()
		L = list(map(int, L))
		return sum(L)


class Solution:

	def findSum(self, s):
		str = ''
		for char in s:
			if char.isalpha():
				str += ' '
			else:
				str += char
		list = str.split()
		final = 0
		for element in list:
			final += int(element)
		return final


class Solution:

	def findSum(self, s):
		digit = ''
		res = 0
		for i in s:
			if i in '0123456789':
				digit += i
			elif digit:
				res += int(digit)
				digit = ''
		if digit:
			res += int(digit)
		return res


class Solution:

	def findSum(self, s):
		import re
		l = re.findall('\\d+', s)
		summ = 0
		for i in l:
			summ += int(i)
		return summ


class Solution:

	def findSum(self, s):
		temp = '0'
		Sum = 0
		for ch in s:
			if ch.isdigit():
				temp += ch
			else:
				Sum += int(temp)
				temp = '0'
		return Sum + int(temp)


class Solution:

	def findSum(self, s):
		output = ''
		for i in s:
			if i.isdigit():
				output += i
			else:
				output += ' '
		total = 0
		x = output.split()
		for i in x:
			num = int(i)
			total += num
		return total


class Solution:

	def findSum(self, s):
		sum = 0
		i = 0
		while i < len(s):
			count = 0
			if s[i].isnumeric() == True:
				j = i
				while j != len(s) - 1 and s[j + 1].isnumeric() == True:
					j = j + 1
					count = count + 1
				num = s[i:j + 1]
				sum = sum + int(num)
			i += count + 1
		return sum


class Solution:

	def findSum(self, s):
		p = ''
		for i in s:
			if i.isdigit():
				p = p + i
			else:
				p = p + ' '
		l = p.split(' ')
		sum = 0
		for i in l:
			if i.isdigit():
				sum += int(i)
		return sum


class Solution:

	def findSum(self, s):
		result = ''
		for char in s:
			if char.isalpha():
				result += '  '
			else:
				result += char
		lst = result.split()
		total = 0
		for each in lst:
			num = int(each)
			total += num
		return total


class Solution:

	def findSum(self, s):
		e = []
		su = ''
		for i in s:
			if i.isdigit():
				su += i
			else:
				if su != '':
					e.append(int(su))
				su = ''
		if su.isdigit():
			e.append(int(su))
		return sum(e)


import re

class Solution:

	def findSum(self, s):
		num = re.findall('\\d+', s)
		s = 0
		for i in num:
			s += int(i)
		return s


class Solution:

	def findSum(self, s):
		num = ''
		Sum = 0
		for i in s:
			if i.isdigit():
				num += i
			elif num:
				Sum += int(num)
				num = ''
		if num:
			Sum += int(num)
		return Sum


class Solution:

	def findSum(self, s):
		ns = ''
		sum = 0
		for i in s:
			if i.isdigit():
				ns += i
			elif ns:
				sum += int(ns)
				ns = ''
		if ns:
			sum += int(ns)
		return sum


class Solution:

	def findSum(self, s):

		def isDigit(d):
			digit = list('0123456789')
			if d in digit:
				return True
			else:
				return False
		ans = 0
		temp = '0'
		for i in range(len(s)):
			if isDigit(s[i]) == True:
				temp += s[i]
			else:
				ans += int(temp)
				temp = '0'
		return ans + int(temp)


class Solution:

	def findSum(self, s):
		for i in s:
			if i.isalpha():
				s = s.replace(i, ' ')
		res = s.split()
		sum = 0
		for i in res:
			sum += int(i)
		return sum


class Solution:

	def findSum(self, S):
		summ = 0
		curr = 0
		i = 0
		while i < len(S):
			if 48 <= ord(S[i]) <= 57:
				curr = int(S[i])
				while i < len(S) - 1 and 48 <= ord(S[i + 1]) <= 57:
					i += 1
					curr = curr * 10 + int(S[i])
				summ += curr
			i += 1
		return summ


class Solution:

	def findSum(self, s):
		S = s
		for i in s:
			if i.isalpha():
				S = S.replace(i, ' ')
		ans = S.split()
		sum = 0
		for num in ans:
			sum += int(num)
		return sum


import re

class Solution:

	def findSum(self, s):
		num = re.findall('\\d+', s)
		for i in range(len(num)):
			num[i] = int(num[i])
		return sum(num)


import re

class Solution:

	def findSum(self, s):
		x = re.findall('[0-9]*[0-9]', s)
		ans = 0
		for y in x:
			ans = ans + int(y)
		return ans


class Solution:

	def findSum(self, s):
		i = 0
		su = 0
		while i < len(s):
			num = '0'
			while i < len(s) and s[i].isdigit():
				num += s[i]
				i += 1
			i += 1
			su += int(num)
		return su


class Solution:

	def findSum(self, s):
		x = ''
		for i in s:
			if i.isalpha():
				x += ' '
			else:
				x += i
		k = x.split()
		ans = 0
		for each in k:
			num = int(each)
			ans += num
		return ans


class Solution:

	def findSum(self, s):
		temp = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
		sum = 0
		hold = ''
		result = []
		for i in range(len(s)):
			if s[i] in temp:
				if i != len(s) - 1:
					if s[i + 1] in temp:
						hold = hold + s[i]
					elif len(hold) > 0 and s[i - 1] in temp:
						hold = hold + s[i]
						result.append(hold)
						hold = ''
					else:
						result.append(s[i])
				elif len(hold) > 0:
					hold = hold + s[i]
					result.append(hold)
				else:
					result.append(s[i])
		for i in range(len(result)):
			sum += int(result[i])
		return sum


class Solution:

	def findSum(self, s):
		n = len(s)
		i = 0
		sum = 0
		while i != n:
			num = 0
			while i != n:
				if ord(s[i]) >= 48 and ord(s[i]) <= 57:
					num = num * 10 + int(s[i])
					i += 1
				else:
					i += 1
					break
			sum += num
		return sum


class Solution:

	def findSum(self, s):
		res = ''
		for ch in s:
			if ch.isalpha():
				res = res + ' '
			else:
				res = res + ch
		lst = res.split()
		tot = 0
		for each in lst:
			num = int(each)
			tot = tot + num
		return tot


import re

class Solution:

	def findSum(self, s):
		list1 = re.findall('[0-9]+', s)
		list2 = [int(i) for i in list1]
		return sum(list2)


import re

class Solution:

	def findSum(self, s):
		l = []
		s1 = ''
		for i in s:
			if ord(i) < 97 or ord(i) > 122:
				s1 = s1 + i
			else:
				l.append(s1)
				s1 = ''
		l.append(s1)
		sum = 0
		for i in l:
			if i == '':
				pass
			else:
				sum = sum + int(i)
		return sum


import re

class Solution:

	def findSum(self, s):
		return sum(map(int, re.findall('\\d+', s)))


class Solution:

	def findSum(self, s):
		t = ''
		for x in s:
			if x.isdigit():
				t += x
			else:
				t += ' '
		t = t.split()
		t = [int(i) for i in t]
		return sum(t)


class Solution:

	def findSum(self, s):
		s += ' '
		Sum = 0
		initial = ''
		for i in s:
			if i.isdigit():
				initial += i
			else:
				if initial != '':
					Sum = Sum + int(initial)
				initial = ''
		return Sum


class Solution:

	def findSum(self, s):
		num = 0
		total = 0
		for c in s:
			if c.isdigit():
				num = num * 10 + int(c)
			else:
				total += num
				num = 0
		total += num
		return total


class Solution:

	def findSum(self, s):
		num = 0
		full_num = 0
		for i in range(len(s)):
			if s[i].isdigit():
				full_num = full_num * 10 + int(s[i])
			else:
				num += full_num
				full_num = 0
		return num + full_num


import re

class Solution:

	def findSum(self, s):
		numbers = re.findall('\\d+', s)
		b = 0
		for i in numbers:
			if i.isdigit():
				b += int(i)
		return b


class Solution:

	def findSum(self, s):
		c = 0
		r = ''
		for i in s:
			if i.isdigit():
				r = r + i
			else:
				r = r + ' '
		x = r.split()
		for j in x:
			k = int(j)
			c = c + k
		return c


class Solution:

	def findSum(self, s):
		t = ''
		P = 0
		for i in s:
			if i.isalpha():
				t += ' '
			else:
				t = t + i
				s1 = t.split()
		for i in s1:
			P += int(i)
		return P


class Solution:

	def findSum(self, s):
		x = ''
		for i in s:
			if i.isalpha():
				x += ' '
			else:
				x += str(i)
		l = x.split(' ')
		ans = 0
		for i in l:
			if i != '':
				ans += int(i)
		return ans


class Solution:

	def findSum(self, s):
		result = 0
		temp = ''
		for i in range(0, len(s)):
			if ord(s[i]) >= 48 and ord(s[i]) <= 57:
				temp += s[i]
			elif temp != '':
				result += int(temp)
				temp = ''
		if temp != '':
			result += int(temp)
			return str(result)
		return str(result)


class Solution:

	def findSum(self, s):
		sum = 0
		strl = '0'
		for i in s:
			if i.isdigit():
				strl += i
			else:
				sum += int(strl)
				strl = '0'
		return sum + int(strl)


class Solution:

	def findSum(self, s):
		sum = 0
		v = []
		strl = ''
		for i in s:
			if i.isdigit():
				strl += i
			elif strl != '':
				x = int(strl)
				v.append(x)
				strl = ''
		if strl != '':
			x = int(strl)
			v.append(x)
		sum = 0
		for i in v:
			sum += i
		return sum


import re

class Solution:

	def findSum(self, s):
		pattern = re.compile('\\d+')
		res_ = sum(map(int, re.findall(pattern, s)))
		return res_


class Solution:

	def findSum(self, s):
		for i in s:
			if i.isalpha():
				s = s.replace(i, ' ', 1)
		s = [int(x) for x in s.split()]
		return sum(s)


class Solution:

	def findSum(self, s):
		a = ''
		for i in s:
			if i.isalpha():
				a += ' '
			else:
				a += i
		lst = a.split()
		sum = 0
		for j in lst:
			res = int(j)
			sum += res
		return sum


class Solution:

	def findSum(self, s):
		res = 0
		num = ''
		for i in range(len(s)):
			if s[i].isnumeric():
				num += s[i]
			elif len(num) > 0:
				res += int(num)
				num = ''
		if len(num) > 0:
			res += int(num)
		return res


class Solution:

	def findSum(self, s):
		lis = []
		k = ''
		for i in s:
			if i.isdigit():
				k += i
			elif k != '':
				lis.append(int(k))
				k = ''
		if k != '':
			lis.append(int(k))
		return sum(lis)


class Solution:

	def findSum(self, s):
		import re
		count = 0
		x = re.findall('\\d+', s)
		for i in range(len(x)):
			count += int(x[i])
		return count


import re

class Solution:

	def findSum(self, s):
		pattern = '\\d+'
		match = re.findall(pattern, s)
		add = 0
		for i in match:
			add += int(i)
		return add


class Solution:

	def findSum(self, s):
		result = 0
		temp = ''
		for i in range(len(s)):
			if s[i].isnumeric():
				temp += s[i]
				if i == len(s) - 1:
					result += int(temp)
			elif temp != '':
				result += int(temp)
				temp = ''
		return result


import re

class Solution:

	def findSum(self, s):
		a = re.findall('\\d+', s)
		c = 0
		for i in a:
			c += int(i)
		return c


class Solution:

	def findSum(self, s):
		ans = ''
		for char in s:
			if char.isalpha():
				ans += ' '
			else:
				ans += char
		l = ans.split()
		res = 0
		for num in l:
			n = int(num)
			res += n
		return res


class Solution:

	def findSum(self, s):
		res = ''
		su = 0
		for i in range(0, len(s)):
			if s[i].isdigit():
				res += s[i]
			else:
				res += ' '
		ans = res.split()
		su = 0
		for ele in ans:
			su += int(ele)
		return su


class Solution:

	def findSum(self, s):
		current_num = 0
		total_sum = 0
		for i in range(len(s)):
			if s[i].isdigit():
				current_num = current_num * 10 + int(s[i])
			else:
				total_sum += current_num
				current_num = 0
		total_sum += current_num
		return total_sum


class Solution:

	def findSum(self, s):
		sum = 0
		st = ''
		for i in s:
			if '0' <= i <= '9':
				st += i
			else:
				st += ' '
		for i in st.split():
			sum += int(i)
		return sum