2117. Abbreviating the Product of a Range LeetCode Solution
In this guide, you will get 2117. Abbreviating the Product of a Range LeetCode Solution with the best time and space complexity. The solution to Abbreviating the Product of a Range problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Abbreviating the Product of a Range solution in C++
- Abbreviating the Product of a Range solution in Java
- Abbreviating the Product of a Range solution in Python
- Additional Resources
Problem Statement of Abbreviating the Product of a Range
You are given two positive integers left and right with left 10, then express the product as
... wheredenotes the first 5 digits of the product, and denotes the last 5 digits of the product after removing all trailing zeros. If d <= 10, we keep it unchanged. For example, we express 1234567654321 as 12345...54321, but 1234567 is represented as 1234567. Finally, represent the product as a string "...eC". For example, 12345678987600000 will be represented as "12345...89876e5". Return a string denoting the abbreviated product of all integers in the inclusive range [left, right].Example 1: Input: left = 1, right = 4 Output: "24e0" Explanation: The product is 1 × 2 × 3 × 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.
Constraints: 1 <= left <= right <= 104
Complexity Analysis
- Time Complexity: O(\texttt{right} - \texttt{left})
- Space Complexity: O(1)
2117. Abbreviating the Product of a Range LeetCode Solution in C++
class Solution {
public:
string abbreviateProduct(int left, int right) {
constexpr long maxSuf = 100'000'000'000;
double prod = 1.0;
long suf = 1;
int countDigits = 0;
int countZeros = 0;
for (int num = left; num <= right; ++num) {
prod *= num;
while (prod >= 1.0) {
prod /= 10;
++countDigits;
}
suf *= num;
while (suf % 10 == 0) {
suf /= 10;
++countZeros;
}
if (suf > maxSuf)
suf %= maxSuf;
}
if (countDigits - countZeros <= 10) {
const long tens = pow(10, countDigits - countZeros);
return to_string(static_cast<long>(prod * tens + 0.5)) + 'e' +
to_string(countZeros);
}
const string pre = to_string(static_cast<long>(prod * pow(10, 5)));
string sufStr = to_string(suf);
sufStr = sufStr.substr(sufStr.length() - 5);
return pre + "..." + sufStr + 'e' + to_string(countZeros);
}
};
/* code provided by PROGIEZ */
2117. Abbreviating the Product of a Range LeetCode Solution in Java
class Solution {
public String abbreviateProduct(int left, int right) {
final long maxSuf = 100_000_000_000L;
double prod = 1.0;
long suf = 1;
int countDigits = 0;
int countZeros = 0;
for (int num = left; num <= right; ++num) {
prod *= num;
while (prod >= 1.0) {
prod /= 10;
++countDigits;
}
suf *= num;
while (suf % 10 == 0) {
suf /= 10;
++countZeros;
}
if (suf > maxSuf)
suf %= maxSuf;
}
if (countDigits - countZeros <= 10) {
final long tens = (long) Math.pow(10, countDigits - countZeros);
return String.valueOf((long) (prod * tens + 0.5)) + "e" + String.valueOf(countZeros);
}
final String pre = String.valueOf((long) (prod * Math.pow(10, 5)));
String sufStr = String.valueOf(suf);
sufStr = sufStr.substring(sufStr.length() - 5);
return pre + "..." + sufStr + "e" + String.valueOf(countZeros);
}
}
// code provided by PROGIEZ
2117. Abbreviating the Product of a Range LeetCode Solution in Python
class Solution:
def abbreviateProduct(self, left: int, right: int) -> str:
prod = 1.0
suf = 1
countDigits = 0
countZeros = 0
for num in range(left, right + 1):
prod *= num
while prod >= 1.0:
prod /= 10
countDigits += 1
suf *= num
while suf % 10 == 0:
suf //= 10
countZeros += 1
if suf > 10**8:
suf %= 10**8
if countDigits - countZeros <= 10:
tens = 10**(countDigits - countZeros)
return str(int(prod * tens + 0.5)) + 'e' + str(countZeros)
pre = str(int(prod * 10 ** 5))
suf = str(suf)[-5:]
return pre + '...' + suf + 'e' + str(countZeros)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.