3448. Count Substrings Divisible By Last Digit Hard

@problem@discussion
#String#Dynamic Programming



1/**
2 * [3448] Count Substrings Divisible By Last Digit
3 *
4 * You are given a string s consisting of digits.
5 * Return the number of <span data-keyword="substring-nonempty">substrings</span> of s divisible by their non-zero last digit.
6 * Note: A substring may contain leading zeros.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">s = "12936"</span>
11 * Output: <span class="example-io">11</span>
12 * Explanation:
13 * 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.
14 * </div>
15 * <strong class="example">Example 2:
16 * <div class="example-block">
17 * Input: <span class="example-io">s = "5701283"</span>
18 * Output: <span class="example-io">18</span>
19 * Explanation:
20 * 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.
21 * </div>
22 * <strong class="example">Example 3:
23 * <div class="example-block">
24 * Input: <span class="example-io">s = "1010101010"</span>
25 * Output: <span class="example-io">25</span>
26 * Explanation:
27 * Only substrings that end with digit '1' are divisible by their last digit. There are 25 such substrings.
28 * </div>
29 *  
30 * Constraints:
31 * 
32 * 	1 <= s.length <= 10^5
33 * 	s consists of digits only.
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/count-substrings-divisible-by-last-digit/
39// discuss: https://leetcode.com/problems/count-substrings-divisible-by-last-digit/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn count_substrings(s: String) -> i64 {
45        
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_3448() {
57    }
58}
59

Back
© 2026 bowen.ge All Rights Reserved.