All problems
0342EasyMath

Trimmed Meter Reading

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3754Concatenate Non-Zero Digits and Multiply by Sum I

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 n from left to right, throw away every digit equal to 0, and read whatever digits are left as a decimal number, keeping their original order. When nothing is left, the trimmed reading is 0.
  • 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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def sum_and_multiply(n: int) -> int:
Java
public long sumAndMultiply(int n)
September 7
Apply