1033. Sum of the Numbers in a String
You are given a string s that contains both digits and lowercase English letters. Your task is to find the sum of all numbers present in the string.
A number is defined as a sequence of one or more consecutive digits. Non-digit characters serve as delimiters.
Write a function to return the sum of all such numbers in the string.
Example 1:
Input: s = "123xyz"
Output: 123
Explanation: One number "123" is found. Sum = 123
Example 2:
Input: s = "1xyz23"
Output: 24
Explanation: Numbers found: 1 and 23. Sum = 1 + 23 = 24
Now Your Turn!
Pick the correct output for the given inputInput: s = "a1b2c3"
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= s.length <= 105
- The string contains only lowercase English letters and digits ('0' to '9')
- The string does not contain any whitespace or special characters