mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 07:21:40 +08:00
51 lines
2.3 KiB
HTML
51 lines
2.3 KiB
HTML
<p>You are given a string <code>s</code> consisting of digits.</p>
|
|
|
|
<p>Return the <strong>number</strong> of <span data-keyword="substring-nonempty">substrings</span> of <code>s</code> <strong>divisible</strong> by their <strong>non-zero</strong> last digit.</p>
|
|
|
|
<p><strong>Note</strong>: A substring may contain leading zeros.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "12936"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">11</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<p>Substrings <code>"29"</code>, <code>"129"</code>, <code>"293"</code> and <code>"2936"</code> are not divisible by their last digit. There are 15 substrings in total, so the answer is <code>15 - 4 = 11</code>.</p>
|
|
</div>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "5701283"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">18</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<p>Substrings <code>"01"</code>, <code>"12"</code>, <code>"701"</code>, <code>"012"</code>, <code>"128"</code>, <code>"5701"</code>, <code>"7012"</code>, <code>"0128"</code>, <code>"57012"</code>, <code>"70128"</code>, <code>"570128"</code>, and <code>"701283"</code> 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 <code>12 + 6 = 18</code>.</p>
|
|
</div>
|
|
|
|
<p><strong class="example">Example 3:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "1010101010"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">25</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<p>Only substrings that end with digit <code>'1'</code> are divisible by their last digit. There are 25 such substrings.</p>
|
|
</div>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
|
<li><code>s</code> consists of digits only.</li>
|
|
</ul>
|