Concatenate Non-Zero Digits and Multiply by Sum II Problem
Description
LeetCode Problem 3756.
You are given a string s of length m consisting of digits. You are also given a 2D integer array queries, where queries[i] = [l_i, r_i].
For each queries[i], extract the substring s[l_i..r_i]. Then, perform the following:
- Form a new integer
xby concatenating all the non-zero digits from the substring in their original order. If there are no non-zero digits,x = 0. - Let
sumbe the sum of digits inx. The answer isx * sum.
Return an array of integers answer where answer[i] is the answer to the i^th query.
Since the answers may be very large, return them modulo 10^9 + 7.
Example 1:
Input: s = "10203004", queries = [[0,7],[1,3],[4,6]]
Output: [12340, 4, 9]
Explanation:
- s[0..7] = "10203004"
- x = 1234
- sum = 1 + 2 + 3 + 4 = 10
- Therefore, answer is 1234 * 10 = 12340.
- s[1..3] = "020"
- x = 2
- sum = 2
- Therefore, the answer is 2 * 2 = 4.
- s[4..6] = "300"
- x = 3
- sum = 3
- Therefore, the answer is 3 * 3 = 9.
Example 2:
Input: s = "1000", queries = [[0,3],[1,1]]
Output: [1, 0]
Explanation:
- s[0..3] = "1000"
- x = 1
- sum = 1
- Therefore, the answer is 1 * 1 = 1.
- s[1..1] = "0"
- x = 0
- sum = 0
- Therefore, the answer is 0 * 0 = 0.
Example 3:
Input: s = "9876543210", queries = [[0,9]]
Output: [444444137]
Explanation:
- s[0..9] = "9876543210"
- x = 987654321
- sum = 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 45
- Therefore, the answer is 987654321 * 45 = 44444444445.
- We return 44444444445 modulo (10^9 + 7) = 444444137.
Constraints:
1 <= m == s.length <= 10^5sconsists of digits only.1 <= queries.length <= 10^5queries[i] = [l_i, r_i]0 <= l_i <= r_i < m
Solution
Approach: Prefix Sums
Each query needs three things about s[l..r]: the digit sum, how many non-zero digits there are, and the number x formed by those digits, modulo 10^9 + 7. Build prefix arrays so that each can be read in O(1):
sumD[i]: the sum of the firstidigits.cntN0[i]: the count of non-zero digits among the firstidigits.p[i]: the number formed by all non-zero digits among the firstidigits, modulo the prime. A zero digit leaves it unchanged; a non-zero digitdturns it intop * 10 + d.
For a query [l, r], let n0 = cntN0[r+1] - cntN0[l] be the number of non-zero digits in the substring. Then p[r+1] = p[l] * 10^n0 + x, so x = p[r+1] - p[l] * 10^n0 (mod the prime), using a precomputed table of powers of 10. The answer is x * (sumD[r+1] - sumD[l]) modulo the prime.
Complexity: O(m + q) time and O(m) space, for a string of length m and q queries.
mx = 10**5 + 1
mod = 10**9 + 7
pow10 = [1] * mx
for i in range(1, mx):
pow10[i] = pow10[i - 1] * 10 % mod
class Solution:
def sumAndMultiply(self, s: str, queries: List[List[int]]) -> List[int]:
n = len(s)
sum_d = [0] * (n + 1)
cnt_n0 = [0] * (n + 1)
p = [0] * (n + 1)
for i, d in enumerate(map(int, s), 1):
sum_d[i] = sum_d[i - 1] + d
cnt_n0[i] = cnt_n0[i - 1] + int(d > 0)
p[i] = (p[i - 1] * 10 + d) % mod if d else p[i - 1]
ans = []
for l, r in queries:
n0 = cnt_n0[r + 1] - cnt_n0[l]
sd = sum_d[r + 1] - sum_d[l]
x = p[r + 1] - p[l] * pow10[n0] % mod
ans.append(x * sd % mod)
return ans