Show problems as
Classic problem #3754EasyMath

Concatenate Non-Zero Digits and Multiply by Sum I

Real-world version: Trimmed Meter Reading
Tracked in this browser only
Read Concatenate Non-Zero Digits and Multiply by Sum I on LeetCode(opens in a new tab)

The 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 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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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