Concatenate Non-Zero Digits and Multiply by Sum II

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:

  • Form a new integer x by concatenating all the non-zero digits from the substring in their original order. If there are no non-zero digits, x = 0.
  • Let 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 i^th query.

Since the answers may be very large, return them modulo 10^9 + 7.

Example 1
Inputs = "10203004", queries = [[0,7],[1,3],[4,6]]
Output[12340,4,9]
For the three substrings, the concatenated non-zero values are 1234, 2, and 3 with digit sums 10, 2, and 3, producing answers 12340, 4, and 9.
Example 2
Inputs = "1000", queries = [[0,3],[1,1]]
Output[1,0]
For s[0..3], x = 1 and sum = 1, while for s[1..1], x = 0 and sum = 0.

Constraints

  • 1 <= m == s.length <= 10^5
  • s consists of digits only.
  • 1 <= queries.length <= 10^5
  • queries[i] = [li, ri]
  • 0 <= li <= ri < m

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate