Concatenate Non-Zero Digits and Multiply by Sum I
Real-world version: Trimmed Meter ReadingThe statement here is our own: the same inputs, outputs and technique.
A water meter prints a whole-number reading n. The billing system turns that reading into a single audit figure from two ingredients.
- The trimmed reading: walk the decimal digits of
nfrom left to right, throw away every digit equal to0, and read whatever digits are left as a decimal number, keeping their original order. When nothing is left, the trimmed reading is0. - The digit total: the sum of all decimal digits of
n.
Return the trimmed reading multiplied by the digit total. The product can be larger than a signed 32-bit integer holds.
Examples
Example 1
- Input
- n = 58306
- Output
- 128392
Dropping the single `0` leaves the digits `5`, `8`, `3`, `6`, so the trimmed reading is 58306 without its zero, namely 5836. The digit total is 5 + 8 + 3 + 0 + 6 = 22, and 5836 * 22 = 128392.
Example 2
- Input
- n = 4907
- Output
- 9940
The trimmed reading is 497 and the digit total is 4 + 9 + 0 + 7 = 20, so the answer is 497 * 20 = 9940.
Example 3
- Input
- n = 91
- Output
- 910
No digit is thrown away, so the trimmed reading equals the reading itself. The digit total is 9 + 1 = 10, and 91 * 10 = 910.
Constraints
- 0 <= n <= 10^9
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def sum_and_multiply(n: int) -> int:public long sumAndMultiply(int n)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's