Given a string s containing a mix of lowercase letters and digits, parse the string and return the sum of all integers found within it.
Consecutive digits form a single integer. For example, in the string "abc123de45f" the integers are 123 and 45, and their sum is 168.
Function Signature
def sum_of_numbers(s: str) -> int:
Rules
-
An integer is a maximal run of consecutive digits, read in base 10.
-
Leading zeros are allowed and do not change the value:
"007"
is 7, and
"0"
is 0.
-
Letters only separate numbers. The input has no signs, spaces or decimal points.
-
Return the exact sum. A digit run can be as long as the string, and many shorter runs can also add up past 64 bits, so the value is not limited to a fixed integer width.
Constraints
-
1 <= len(s) <= 10^4
-
s
consists of lowercase English letters and digits (
0-9
).
-
There is at least one digit in
s
.
Examples
Input: s = "abc123de45f"
Output: 168
The digit runs are "123" and "45", and 123 + 45 = 168.
Input: s = "a1b2c3"
Output: 6
Each digit is its own run because letters separate them, so the sum is 1 + 2 + 3 = 6.
Input: s = "007x10"
Output: 17
The run "007" has the value 7 and the string ends inside the run "10", which still counts, so the sum is 7 + 10 = 17.