Sum the Numbers in a String - FreeCodeCamp #142 Daily Challenge
2 min
Sum The String - Analysis and Explanation
Problem Statement
Given a string containing digits and other characters, the goal is to return the sum of all numbers present in the string.
- Consecutive digits form a single number. For example,
"13"counts as 13, not as . - All other non-numeric characters should be ignored.
Initial Analysis
Understanding the Problem
The key lies in identifying uninterrupted sequences of digits as a single numerical unit. We are not summing individual digits, but rather numeric blocks.
Identified Test Cases
stringSum("3apples2bananas")→5().stringSum("10cats5dogs2birds")→17().stringSum("125344")→125344.stringSum("a1b20c300")→321().
Edge cases:
stringSum("")→0(empty string).stringSum("abc")→0(no numbers).stringSum("0a0b0")→0(explicit zeros).
Solution Development
Approach: Regular Expressions
We will use a regular expression (/\d+/g) to capture all sequences of one or more digits. This approach is the most efficient and readable in JavaScript/TypeScript for pattern extraction tasks.
Solution Flow
flowchart TD
A["Input: String"] --> B["Apply RegExp: /\d+/g"]
B --> C{Any matches?}
C -- No --> D["Return 0"]
C -- Yes --> E["Convert strings to numbers"]
E --> F["Sum with reduce()"]
F --> G["Final result"]
Implementation
/**
* Sums all numbers found in a string.
* @param str Text string to process.
* @returns The total sum of the numbers found.
*/
function stringSum(str: string): number {
// Look for sequences of one or more digits (\d+) globally (g)
const matches = str.match(/\d+/g)
if (!matches) {
return 0
}
// Convert each match to a number and accumulate
return matches.reduce((acc, num) => acc + Number(num), 0)
}
export default stringSumComplexity Analysis
Time Complexity
- : Where is the length of the input string. The regular expression engine must traverse the entire string once to find all matches. Subsequently, the
reducemethod iterates through the found matches, which is proportional to in the worst case.
Space Complexity
- : In the worst case (a string composed of numbers separated by a single non-numeric character), the matches array could occupy space proportional to the size of the original string.
Edge Cases and Considerations
- No numbers: If the string contains no digits,
matchreturnsnull. We handle this by returning0immediately. - Large numbers: JavaScript handles integers safely up to
Number.MAX_SAFE_INTEGER(). For larger values,BigIntwould be required. - Zeros: Leading zeros in sequences like
"007"are correctly treated byNumber()as7.
Reflections and Lessons Learned
Key Concepts
- RegExp: Using quantifiers (
+) and flags (g) for global search. - Higher-Order Functions: Using
reducefor clean and declarative accumulations. - Nullish Handling: Importance of validating the
match()return value before operating.