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.
Given a binary string s, return the number of substrings with all characters 1's. Since the answer may be too large, return it modulo 109 + 7.
Example 1:
Input: s = "0110111" Output: 9 Explanation: There are 9 substring in total with only 1's characters. "1" -> 5 times. "11" -> 3 times. "111" -> 1 time.
Example 2:
Input: s = "101" Output: 2 Explanation: Substring "1" is shown 2 times in s.
Example 3:
Input: s = "111111" Output: 21 Explanation: Each substring contains only 1's characters.
Constraints:
1 <= s.length <= 105s[i] is either '0' or '1'.Problem summary: Given a binary string s, return the number of substrings with all characters 1's. Since the answer may be too large, return it modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
"0110111"
"101"
"111111"
count-number-of-homogenous-substrings)count-vowel-substrings-of-a-string)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1513: Number of Substrings With Only 1s
class Solution {
public int numSub(String s) {
final int mod = 1_000_000_007;
int ans = 0, cur = 0;
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == '0') {
cur = 0;
} else {
cur++;
ans = (ans + cur) % mod;
}
}
return ans;
}
}
// Accepted solution for LeetCode #1513: Number of Substrings With Only 1s
func numSub(s string) (ans int) {
const mod int = 1e9 + 7
cur := 0
for _, c := range s {
if c == '0' {
cur = 0
} else {
cur++
ans = (ans + cur) % mod
}
}
return
}
# Accepted solution for LeetCode #1513: Number of Substrings With Only 1s
class Solution:
def numSub(self, s: str) -> int:
mod = 10**9 + 7
ans = cur = 0
for c in s:
if c == "0":
cur = 0
else:
cur += 1
ans = (ans + cur) % mod
return ans
// Accepted solution for LeetCode #1513: Number of Substrings With Only 1s
impl Solution {
pub fn num_sub(s: String) -> i32 {
const MOD: i32 = 1_000_000_007;
let mut ans: i32 = 0;
let mut cur: i32 = 0;
for c in s.chars() {
if c == '0' {
cur = 0;
} else {
cur += 1;
ans = (ans + cur) % MOD;
}
}
ans
}
}
// Accepted solution for LeetCode #1513: Number of Substrings With Only 1s
function numSub(s: string): number {
const mod = 1_000_000_007;
let [ans, cur] = [0, 0];
for (const c of s) {
if (c === '0') {
cur = 0;
} else {
cur++;
ans = (ans + cur) % 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.