Sum all integers embedded in a string of letters and digits

Quick Overview

Parse a string of lowercase letters and digits and return the sum of every maximal run of digits read as an integer. It tests careful parsing of digit runs, boundary handling, and how the code copes with very large values.

Sum all integers embedded in a string of letters and digits

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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 ```python 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 ```text Input: s = "abc123de45f" Output: 168 ``` The digit runs are `"123"` and `"45"`, and 123 + 45 = 168. ```text Input: s = "a1b2c3" Output: 6 ``` Each digit is its own run because letters separate them, so the sum is 1 + 2 + 3 = 6. ```text 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.

Overview: Parse a string of lowercase letters and digits and return the sum of every maximal run of digits read as an integer. It tests careful parsing of digit runs, boundary handling, and how the code copes with very large values.

General logo
General
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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.