You are given a string s of length m consisting of digits. You are also given a 2D integer array queries, where queries[i] = [li, ri].
For each queries[i], extract the substring s[li..ri]. Then, perform the following:
x by concatenating all the non-zero digits from the substring in their original order. If there are no non-zero digits, x = 0.sum be the sum of digits in x. The answer is x * sum.Return an array of integers answer where answer[i] is the answer to the ith query.
Since the answers may be very large, return them modulo 109 + 7.
Example 1:
Input: s = "10203004", queries = [[0,7],[1,3],[4,6]]
Output: [12340, 4, 9]
Explanation:
s[0..7] = "10203004"
x = 1234sum = 1 + 2 + 3 + 4 = 101234 * 10 = 12340.s[1..3] = "020"
x = 2sum = 22 * 2 = 4.s[4..6] = "300"
x = 3sum = 33 * 3 = 9.Example 2:
Input: s = "1000", queries = [[0,3],[1,1]]
Output: [1, 0]
Explanation:
s[0..3] = "1000"
x = 1sum = 11 * 1 = 1.s[1..1] = "0"
x = 0sum = 00 * 0 = 0.Example 3:
Input: s = "9876543210", queries = [[0,9]]
Output: [444444137]
Explanation:
s[0..9] = "9876543210"
x = 987654321sum = 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 45987654321 * 45 = 44444444445.44444444445 modulo (109 + 7) = 444444137.
Constraints:
1 <= m == s.length <= 105s consists of digits only.1 <= queries.length <= 105queries[i] = [li, ri]0 <= li <= ri < mProblem Overview: You process a sequence of digits and repeatedly form a number by concatenating only the non-zero digits while also tracking the running sum of digits. The task is to compute the required multiplied value efficiently without repeatedly rebuilding strings or recomputing sums.
Approach 1: Brute Force Concatenation and Recompute Sum (O(n^2) time, O(n) space)
The direct approach simulates the process literally. For every position, rebuild the number formed by concatenating all non-zero digits seen so far and recompute the sum of digits from scratch. This involves scanning the prefix each time and performing string concatenation or numeric construction. The repeated traversal causes the runtime to grow quadratically for long inputs. This approach is useful for validating correctness on small inputs but becomes inefficient quickly.
Approach 2: Prefix Sum + Rolling Concatenation (O(n) time, O(1) space)
A more efficient method processes the digits in a single pass. Maintain a running prefix sum of digits and a rolling number representing the concatenation of all non-zero digits encountered so far. When the current digit is non-zero, update the rolling value using value = value * 10 + digit. The running sum is updated using a standard prefix sum update. The required result at each step can then be computed directly using these maintained values without reconstructing previous digits. This eliminates repeated scans and reduces the runtime to linear.
This technique combines simple arithmetic with string-style digit processing and prefix sum accumulation. The key insight is that concatenation can be simulated numerically and the cumulative sum can be maintained incrementally.
Recommended for interviews: The prefix-sum plus rolling-number approach is the expected solution. The brute force version shows that you understand the direct interpretation of the problem, but interviewers typically look for the optimization that avoids rebuilding prefixes. Using incremental updates and constant extra space demonstrates stronger algorithmic thinking and familiarity with math-based digit manipulation.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Concatenation | O(n^2) | O(n) | Useful for understanding the direct process or validating logic on small inputs |
| Prefix Sum + Rolling Number | O(n) | O(1) | Preferred solution for large inputs and interview settings |
Leetcode 3756 🔥 Concatenate Non-Zero Digits & Multiply by Sum 2 | Weekly Contest 477 Q3 | Prefix • Study Placement • 535 views views
Watch 4 more video solutions →Practice Concatenate Non-Zero Digits and Multiply by Sum II with our built-in code editor and test cases.
Practice on FleetCode