You are given a string s consisting of digits.
Return the number of substrings of s divisible by their non-zero last digit.
Note: A substring may contain leading zeros.
Example 1:
Input: s = "12936"
Output: 11
Explanation:
Substrings "29", "129", "293" and "2936" are not divisible by their last digit. There are 15 substrings in total, so the answer is 15 - 4 = 11.
Example 2:
Input: s = "5701283"
Output: 18
Explanation:
Substrings "01", "12", "701", "012", "128", "5701", "7012", "0128", "57012", "70128", "570128", and "701283" are all divisible by their last digit. Additionally, all substrings that are just 1 non-zero digit are divisible by themselves. Since there are 6 such digits, the answer is 12 + 6 = 18.
Example 3:
Input: s = "1010101010"
Output: 25
Explanation:
Only substrings that end with digit '1' are divisible by their last digit. There are 25 such substrings.
Constraints:
1 <= s.length <= 105s consists of digits only.Problem Overview: Given a string of digits, count every substring where the integer value of the substring is divisible by its last digit. Substrings ending with digit 0 are ignored because division by zero is invalid. The challenge is evaluating divisibility efficiently without converting every substring to an integer.
Approach 1: Brute Force Enumeration (O(n^2) time, O(1) space)
Generate every substring ending at each index and compute its numeric value incrementally. For each ending index i, extend the substring backward and maintain the current value using base‑10 expansion. If the last digit d is non‑zero and the substring value modulo d equals zero, increment the count. This approach directly follows the problem definition but requires checking up to O(n^2) substrings, which becomes too slow for large inputs.
Approach 2: Dynamic Programming with Remainder States (O(n) time, O(1) space)
The key observation: the last digit d determines the divisibility rule. Since d ranges from 1 to 9, track substring remainders for each possible modulus. Maintain counts of substrings ending at the current position with remainder r modulo d. When a new digit x appears, update remainders using the transition new_r = (old_r * 10 + x) % d. Also start a new single‑digit substring at the current index.
This effectively builds all substrings ending at the current index without explicitly enumerating them. For the current last digit d, any substring ending here with remainder 0 modulo d is valid. Because d is at most 9, the number of states is constant, making the algorithm linear. The technique is a classic remainder DP pattern often used in dynamic programming problems involving divisibility.
The algorithm iterates through the string once, updating remainder counts for moduli 1 through 9. Each step processes a constant number of states, so the total complexity stays O(n) with constant memory.
Recommended for interviews: Start by explaining the brute force idea to demonstrate understanding of substring enumeration. Then pivot to the optimized remainder‑tracking DP. Interviewers expect candidates to recognize that the divisor is limited (1–9) and exploit that constraint to keep only a small set of remainder states. This shows strong problem‑solving and optimization skills.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Substring Enumeration | O(n^2) | O(1) | Small input sizes or when first reasoning about the problem |
| Dynamic Programming with Remainder Tracking | O(n) | O(1) | General case and interview‑ready optimal solution |
3448. Count Substrings Divisible By Last Digit | 3D DP | With Dry Run • Aryan Mittal • 3,469 views views
Watch 6 more video solutions →Practice Count Substrings Divisible By Last Digit with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor