#2117

Abbreviating the Product of a Range

international master · 2145 · lc hard +32 · verified · 24.9% accepted · 91 likes · top 4%

Description

You are given two positive integers left and right with left <= right. Compute the product of all integers in the inclusive range [left, right].

Because the product can be enormous, abbreviate it using these steps:

- Count and strip all trailing zeros from the product, calling the count C.

- For example, 1000 has 3 trailing zeros; 546 has none.

- Let d be the number of digits remaining after removing trailing zeros. If d > 10, express the product as <pre>...<suf> where <pre> is the first 5 digits and <suf> is the last 5 digits. If d <= 10, keep the full number.

- For example, 1234567654321 becomes 12345...54321, while 1234567 stays 1234567.

- Represent the final result as "<pre>...<suf>eC".

- For example, 12345678987600000 becomes "12345...89876e5".

Return the abbreviated product string for the range [left, right].

Example 1:

Input: left = 1, right = 4
Output: "24e0"
Explanation: The product is 1 &times; 2 &times; 3 &times; 4 = 24.
There are no trailing zeros, so 24 remains the same. The abbreviation will end with "e0".
Since the number of digits is 2, which is less than 10, we do not have to abbreviate it further.
Thus, the final representation is "24e0".

Example 2:

Input: left = 2, right = 11
Output: "399168e2"
Explanation: The product is 39916800.
There are 2 trailing zeros, which we remove to get 399168. The abbreviation will end with "e2".
The number of digits after removing the trailing zeros is 6, so we do not abbreviate it further.
Hence, the abbreviated product is "399168e2".

Example 3:

Input: left = 371, right = 375
Output: "7219856259e3"
Explanation: The product is 7219856259000.

Code

1
2
3