Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
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 summary: 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 ith query. Since the answers may be very large, return them modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
"10203004" [[0,7],[1,3],[4,6]]
"1000" [[0,3],[1,1]]
"9876543210" [[0,9]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3756: Concatenate Non-Zero Digits and Multiply by Sum II
// Auto-generated Java example from go.
class Solution {
public void exampleSolution() {
}
}
// Reference (go):
// // Accepted solution for LeetCode #3756: Concatenate Non-Zero Digits and Multiply by Sum II
// package main
//
// // https://space.bilibili.com/206214
// const mod = 1_000_000_007
// const maxN = 100_001
//
// var pow10 = [maxN]int{1}
//
// func init() {
// // 预处理 10 的幂
// for i := 1; i < maxN; i++ {
// pow10[i] = pow10[i-1] * 10 % mod
// }
// }
//
// func sumAndMultiply(s string, queries [][]int) []int {
// n := len(s)
// sumD := make([]int, n+1) // s 的前缀和
// preNum := make([]int, n+1) // s 的前缀对应的数字(模 mod)
// sumNonZero := make([]int, n+1) // s 的前缀中的非零数字个数
// for i, ch := range s {
// d := int(ch - '0')
// sumD[i+1] = sumD[i] + d
// preNum[i+1] = preNum[i]
// sumNonZero[i+1] = sumNonZero[i]
// if d > 0 {
// preNum[i+1] = (preNum[i]*10 + d) % mod
// sumNonZero[i+1]++
// }
// }
//
// ans := make([]int, len(queries))
// for i, q := range queries {
// l, r := q[0], q[1]+1
// length := sumNonZero[r] - sumNonZero[l]
// x := preNum[r] - preNum[l]*pow10[length]%mod + mod // +mod 保证结果非负
// ans[i] = x * (sumD[r] - sumD[l]) % mod
// }
// return ans
// }
// Accepted solution for LeetCode #3756: Concatenate Non-Zero Digits and Multiply by Sum II
package main
// https://space.bilibili.com/206214
const mod = 1_000_000_007
const maxN = 100_001
var pow10 = [maxN]int{1}
func init() {
// 预处理 10 的幂
for i := 1; i < maxN; i++ {
pow10[i] = pow10[i-1] * 10 % mod
}
}
func sumAndMultiply(s string, queries [][]int) []int {
n := len(s)
sumD := make([]int, n+1) // s 的前缀和
preNum := make([]int, n+1) // s 的前缀对应的数字(模 mod)
sumNonZero := make([]int, n+1) // s 的前缀中的非零数字个数
for i, ch := range s {
d := int(ch - '0')
sumD[i+1] = sumD[i] + d
preNum[i+1] = preNum[i]
sumNonZero[i+1] = sumNonZero[i]
if d > 0 {
preNum[i+1] = (preNum[i]*10 + d) % mod
sumNonZero[i+1]++
}
}
ans := make([]int, len(queries))
for i, q := range queries {
l, r := q[0], q[1]+1
length := sumNonZero[r] - sumNonZero[l]
x := preNum[r] - preNum[l]*pow10[length]%mod + mod // +mod 保证结果非负
ans[i] = x * (sumD[r] - sumD[l]) % mod
}
return ans
}
# Accepted solution for LeetCode #3756: Concatenate Non-Zero Digits and Multiply by Sum II
# Time: O(n)
# Space: O(n)
# prefix sum
class Solution(object):
def sumAndMultiply(self, s, queries):
"""
:type s: str
:type queries: List[List[int]]
:rtype: List[int]
"""
MOD = 10**9+7
pow10 = [0]*(len(s)+1)
pow10[0] = 1
idx = [0]*(len(s)+1)
x = [0]*(len(s)+1)
total = [0]*(len(s)+1)
for i in xrange(len(s)):
d = ord(s[i])-ord('0')
pow10[i+1] = (pow10[i]*10)%MOD
idx[i+1] = idx[i]+(1 if d else 0)
x[i+1] = (x[i]*10+d) % MOD if d else x[i]
total[i+1] = total[i]+d
return [(((x[r+1]-x[l]*pow10[idx[r+1]-idx[l]])%MOD)*(total[r+1]-total[l]))%MOD for l, r in queries]
// Accepted solution for LeetCode #3756: Concatenate Non-Zero Digits and Multiply by Sum II
// Rust example auto-generated from go reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (go):
// // Accepted solution for LeetCode #3756: Concatenate Non-Zero Digits and Multiply by Sum II
// package main
//
// // https://space.bilibili.com/206214
// const mod = 1_000_000_007
// const maxN = 100_001
//
// var pow10 = [maxN]int{1}
//
// func init() {
// // 预处理 10 的幂
// for i := 1; i < maxN; i++ {
// pow10[i] = pow10[i-1] * 10 % mod
// }
// }
//
// func sumAndMultiply(s string, queries [][]int) []int {
// n := len(s)
// sumD := make([]int, n+1) // s 的前缀和
// preNum := make([]int, n+1) // s 的前缀对应的数字(模 mod)
// sumNonZero := make([]int, n+1) // s 的前缀中的非零数字个数
// for i, ch := range s {
// d := int(ch - '0')
// sumD[i+1] = sumD[i] + d
// preNum[i+1] = preNum[i]
// sumNonZero[i+1] = sumNonZero[i]
// if d > 0 {
// preNum[i+1] = (preNum[i]*10 + d) % mod
// sumNonZero[i+1]++
// }
// }
//
// ans := make([]int, len(queries))
// for i, q := range queries {
// l, r := q[0], q[1]+1
// length := sumNonZero[r] - sumNonZero[l]
// x := preNum[r] - preNum[l]*pow10[length]%mod + mod // +mod 保证结果非负
// ans[i] = x * (sumD[r] - sumD[l]) % mod
// }
// return ans
// }
// Accepted solution for LeetCode #3756: Concatenate Non-Zero Digits and Multiply by Sum II
// Auto-generated TypeScript example from go.
function exampleSolution(): void {
}
// Reference (go):
// // Accepted solution for LeetCode #3756: Concatenate Non-Zero Digits and Multiply by Sum II
// package main
//
// // https://space.bilibili.com/206214
// const mod = 1_000_000_007
// const maxN = 100_001
//
// var pow10 = [maxN]int{1}
//
// func init() {
// // 预处理 10 的幂
// for i := 1; i < maxN; i++ {
// pow10[i] = pow10[i-1] * 10 % mod
// }
// }
//
// func sumAndMultiply(s string, queries [][]int) []int {
// n := len(s)
// sumD := make([]int, n+1) // s 的前缀和
// preNum := make([]int, n+1) // s 的前缀对应的数字(模 mod)
// sumNonZero := make([]int, n+1) // s 的前缀中的非零数字个数
// for i, ch := range s {
// d := int(ch - '0')
// sumD[i+1] = sumD[i] + d
// preNum[i+1] = preNum[i]
// sumNonZero[i+1] = sumNonZero[i]
// if d > 0 {
// preNum[i+1] = (preNum[i]*10 + d) % mod
// sumNonZero[i+1]++
// }
// }
//
// ans := make([]int, len(queries))
// for i, q := range queries {
// l, r := q[0], q[1]+1
// length := sumNonZero[r] - sumNonZero[l]
// x := preNum[r] - preNum[l]*pow10[length]%mod + mod // +mod 保证结果非负
// ans[i] = x * (sumD[r] - sumD[l]) % mod
// }
// return ans
// }
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.