Skip to content

Commit 5ae0da4

Browse files
authored
feat: add solutions to lc problem: No.2950 (doocs#2040)
No.2950.Number of Divisible Substrings
1 parent b00dfb3 commit 5ae0da4

File tree

12 files changed

+755
-0
lines changed

12 files changed

+755
-0
lines changed
Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
# [2950. Number of Divisible Substrings](https://leetcode.cn/problems/number-of-divisible-substrings)
2+
3+
[English Version](/solution/2900-2999/2950.Number%20of%20Divisible%20Substrings/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Each character of the English alphabet has been mapped to a digit as shown below.</p>
10+
11+
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/11/28/old_phone_digits.png" style="padding: 10px; width: 200px; height: 200px;" /></p>
12+
13+
<p>A string is <strong>divisible</strong> if the sum of the mapped values of its characters is divisible by its length.</p>
14+
15+
<p>Given a string <code>s</code>, return <em>the number of <strong>divisible substrings</strong> of</em> <code>s</code>.</p>
16+
17+
<p>A <strong>substring</strong> is a contiguous non-empty sequence of characters within a string.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
23+
<tbody>
24+
<tr>
25+
<th style="padding: 5px; border: 1px solid black;">Substring</th>
26+
<th style="padding: 5px; border: 1px solid black;">Mapped</th>
27+
<th style="padding: 5px; border: 1px solid black;">Sum</th>
28+
<th style="padding: 5px; border: 1px solid black;">Length</th>
29+
<th style="padding: 5px; border: 1px solid black;">Divisible?</th>
30+
</tr>
31+
<tr>
32+
<td style="padding: 5px; border: 1px solid black;">a</td>
33+
<td style="padding: 5px; border: 1px solid black;">1</td>
34+
<td style="padding: 5px; border: 1px solid black;">1</td>
35+
<td style="padding: 5px; border: 1px solid black;">1</td>
36+
<td style="padding: 5px; border: 1px solid black;">Yes</td>
37+
</tr>
38+
<tr>
39+
<td style="padding: 5px; border: 1px solid black;">s</td>
40+
<td style="padding: 5px; border: 1px solid black;">7</td>
41+
<td style="padding: 5px; border: 1px solid black;">7</td>
42+
<td style="padding: 5px; border: 1px solid black;">1</td>
43+
<td style="padding: 5px; border: 1px solid black;">Yes</td>
44+
</tr>
45+
<tr>
46+
<td style="padding: 5px; border: 1px solid black;">d</td>
47+
<td style="padding: 5px; border: 1px solid black;">2</td>
48+
<td style="padding: 5px; border: 1px solid black;">2</td>
49+
<td style="padding: 5px; border: 1px solid black;">1</td>
50+
<td style="padding: 5px; border: 1px solid black;">Yes</td>
51+
</tr>
52+
<tr>
53+
<td style="padding: 5px; border: 1px solid black;">f</td>
54+
<td style="padding: 5px; border: 1px solid black;">3</td>
55+
<td style="padding: 5px; border: 1px solid black;">3</td>
56+
<td style="padding: 5px; border: 1px solid black;">1</td>
57+
<td style="padding: 5px; border: 1px solid black;">Yes</td>
58+
</tr>
59+
<tr>
60+
<td style="padding: 5px; border: 1px solid black;">as</td>
61+
<td style="padding: 5px; border: 1px solid black;">1, 7</td>
62+
<td style="padding: 5px; border: 1px solid black;">8</td>
63+
<td style="padding: 5px; border: 1px solid black;">2</td>
64+
<td style="padding: 5px; border: 1px solid black;">Yes</td>
65+
</tr>
66+
<tr>
67+
<td style="padding: 5px; border: 1px solid black;">sd</td>
68+
<td style="padding: 5px; border: 1px solid black;">7, 2</td>
69+
<td style="padding: 5px; border: 1px solid black;">9</td>
70+
<td style="padding: 5px; border: 1px solid black;">2</td>
71+
<td style="padding: 5px; border: 1px solid black;">No</td>
72+
</tr>
73+
<tr>
74+
<td style="padding: 5px; border: 1px solid black;">df</td>
75+
<td style="padding: 5px; border: 1px solid black;">2, 3</td>
76+
<td style="padding: 5px; border: 1px solid black;">5</td>
77+
<td style="padding: 5px; border: 1px solid black;">2</td>
78+
<td style="padding: 5px; border: 1px solid black;">No</td>
79+
</tr>
80+
<tr>
81+
<td style="padding: 5px; border: 1px solid black;">asd</td>
82+
<td style="padding: 5px; border: 1px solid black;">1, 7, 2</td>
83+
<td style="padding: 5px; border: 1px solid black;">10</td>
84+
<td style="padding: 5px; border: 1px solid black;">3</td>
85+
<td style="padding: 5px; border: 1px solid black;">No</td>
86+
</tr>
87+
<tr>
88+
<td style="padding: 5px; border: 1px solid black;">sdf</td>
89+
<td style="padding: 5px; border: 1px solid black;">7, 2, 3</td>
90+
<td style="padding: 5px; border: 1px solid black;">12</td>
91+
<td style="padding: 5px; border: 1px solid black;">3</td>
92+
<td style="padding: 5px; border: 1px solid black;">Yes</td>
93+
</tr>
94+
<tr>
95+
<td style="padding: 5px; border: 1px solid black;">asdf</td>
96+
<td style="padding: 5px; border: 1px solid black;">1, 7, 2, 3</td>
97+
<td style="padding: 5px; border: 1px solid black;">13</td>
98+
<td style="padding: 5px; border: 1px solid black;">4</td>
99+
<td style="padding: 5px; border: 1px solid black;">No</td>
100+
</tr>
101+
</tbody>
102+
</table>
103+
104+
<pre>
105+
<strong>Input:</strong> word = &quot;asdf&quot;
106+
<strong>Output:</strong> 6
107+
<strong>Explanation:</strong> The table above contains the details about every substring of word, and we can see that 6 of them are divisible.
108+
</pre>
109+
110+
<p><strong class="example">Example 2:</strong></p>
111+
112+
<pre>
113+
<strong>Input:</strong> word = &quot;bdh&quot;
114+
<strong>Output:</strong> 4
115+
<strong>Explanation:</strong> The 4 divisible substrings are: &quot;b&quot;, &quot;d&quot;, &quot;h&quot;, &quot;bdh&quot;.
116+
It can be shown that there are no other substrings of word that are divisible.
117+
</pre>
118+
119+
<p><strong class="example">Example 3:</strong></p>
120+
121+
<pre>
122+
<strong>Input:</strong> word = &quot;abcd&quot;
123+
<strong>Output:</strong> 6
124+
<strong>Explanation:</strong> The 6 divisible substrings are: &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;ab&quot;, &quot;cd&quot;.
125+
It can be shown that there are no other substrings of word that are divisible.
126+
</pre>
127+
128+
<p>&nbsp;</p>
129+
<p><strong>Constraints:</strong></p>
130+
131+
<ul>
132+
<li><code>1 &lt;= word.length &lt;= 2000</code></li>
133+
<li><code>word</code> consists only of lowercase English letters.</li>
134+
</ul>
135+
136+
## 解法
137+
138+
<!-- 这里可写通用的实现逻辑 -->
139+
140+
**方法一:枚举**
141+
142+
我们先用一个哈希表或数组 $mp$ 记录每个字母对应的数字。
143+
144+
然后,我们枚举子串的起始位置 $i$,再枚举子串的结束位置 $j$,计算子串 $s[i..j]$ 的数字和 $s$,如果 $s$ 能被 $j-i+1$ 整除,那么就找到了一个可被整除的子串,将答案加一。
145+
146+
枚举结束后,返回答案。
147+
148+
时间复杂度 $O(n^2)$,空间复杂度 $O(C)$。其中 $n$ 是字符串 $word$ 的长度,而 $C$ 是字符集的大小,本题中 $C=26$。
149+
150+
<!-- tabs:start -->
151+
152+
### **Python3**
153+
154+
<!-- 这里可写当前语言的特殊实现逻辑 -->
155+
156+
```python
157+
class Solution:
158+
def countDivisibleSubstrings(self, word: str) -> int:
159+
d = ["ab", "cde", "fgh", "ijk", "lmn", "opq", "rst", "uvw", "xyz"]
160+
mp = {}
161+
for i, s in enumerate(d, 1):
162+
for c in s:
163+
mp[c] = i
164+
ans = 0
165+
n = len(word)
166+
for i in range(n):
167+
s = 0
168+
for j in range(i, n):
169+
s += mp[word[j]]
170+
ans += s % (j - i + 1) == 0
171+
return ans
172+
```
173+
174+
### **Java**
175+
176+
<!-- 这里可写当前语言的特殊实现逻辑 -->
177+
178+
```java
179+
class Solution {
180+
public int countDivisibleSubstrings(String word) {
181+
String[] d = {"ab", "cde", "fgh", "ijk", "lmn", "opq", "rst", "uvw", "xyz"};
182+
int[] mp = new int[26];
183+
for (int i = 0; i < d.length; ++i) {
184+
for (char c : d[i].toCharArray()) {
185+
mp[c - 'a'] = i + 1;
186+
}
187+
}
188+
int ans = 0;
189+
int n = word.length();
190+
for (int i = 0; i < n; ++i) {
191+
int s = 0;
192+
for (int j = i; j < n; ++j) {
193+
s += mp[word.charAt(j) - 'a'];
194+
ans += s % (j - i + 1) == 0 ? 1 : 0;
195+
}
196+
}
197+
return ans;
198+
}
199+
}
200+
```
201+
202+
### **C++**
203+
204+
```cpp
205+
class Solution {
206+
public:
207+
int countDivisibleSubstrings(string word) {
208+
string d[9] = {"ab", "cde", "fgh", "ijk", "lmn", "opq", "rst", "uvw", "xyz"};
209+
int mp[26]{};
210+
for (int i = 0; i < 9; ++i) {
211+
for (char& c : d[i]) {
212+
mp[c - 'a'] = i + 1;
213+
}
214+
}
215+
int ans = 0;
216+
int n = word.size();
217+
for (int i = 0; i < n; ++i) {
218+
int s = 0;
219+
for (int j = i; j < n; ++j) {
220+
s += mp[word[j] - 'a'];
221+
ans += s % (j - i + 1) == 0 ? 1 : 0;
222+
}
223+
}
224+
return ans;
225+
}
226+
};
227+
```
228+
229+
### **Go**
230+
231+
```go
232+
func countDivisibleSubstrings(word string) (ans int) {
233+
d := []string{"ab", "cde", "fgh", "ijk", "lmn", "opq", "rst", "uvw", "xyz"}
234+
mp := [26]int{}
235+
for i, s := range d {
236+
for _, c := range s {
237+
mp[c-'a'] = i + 1
238+
}
239+
}
240+
n := len(word)
241+
for i := 0; i < n; i++ {
242+
s := 0
243+
for j := i; j < n; j++ {
244+
s += mp[word[j]-'a']
245+
if s%(j-i+1) == 0 {
246+
ans++
247+
}
248+
}
249+
}
250+
return
251+
}
252+
```
253+
254+
### **TypeScript**
255+
256+
```ts
257+
function countDivisibleSubstrings(word: string): number {
258+
const d: string[] = ['ab', 'cde', 'fgh', 'ijk', 'lmn', 'opq', 'rst', 'uvw', 'xyz'];
259+
const mp: number[] = Array(26).fill(0);
260+
for (let i = 0; i < d.length; ++i) {
261+
for (const c of d[i]) {
262+
mp[c.charCodeAt(0) - 'a'.charCodeAt(0)] = i + 1;
263+
}
264+
}
265+
const n = word.length;
266+
let ans = 0;
267+
for (let i = 0; i < n; ++i) {
268+
let s = 0;
269+
for (let j = i; j < n; ++j) {
270+
s += mp[word.charCodeAt(j) - 'a'.charCodeAt(0)];
271+
if (s % (j - i + 1) === 0) {
272+
++ans;
273+
}
274+
}
275+
}
276+
return ans;
277+
}
278+
```
279+
280+
### **Rust**
281+
282+
```rust
283+
impl Solution {
284+
pub fn count_divisible_substrings(word: String) -> i32 {
285+
let d = vec!["ab", "cde", "fgh", "ijk", "lmn", "opq", "rst", "uvw", "xyz"];
286+
let mut mp = vec![0; 26];
287+
288+
for (i, s) in d.iter().enumerate() {
289+
s.chars().for_each(|c| {
290+
mp[(c as usize) - ('a' as usize)] = (i + 1) as i32;
291+
});
292+
}
293+
294+
let mut ans = 0;
295+
let n = word.len();
296+
297+
for i in 0..n {
298+
let mut s = 0;
299+
300+
for j in i..n {
301+
s += mp[(word.as_bytes()[j] as usize) - ('a' as usize)];
302+
ans += (s % ((j - i + 1) as i32) == 0) as i32;
303+
}
304+
}
305+
306+
ans
307+
}
308+
}
309+
```
310+
311+
### **...**
312+
313+
```
314+
315+
```
316+
317+
<!-- tabs:end -->

0 commit comments

Comments
 (0)