Substring | Mapped | Sum | Length | Divisible?---|---|---|---|---a |1|1|1| Yes
s |7|7|1| Yes
d |2|2|1| Yes
f |3|3|1| Yes
as |1,7|8|2| Yes
sd |7,2|9|2| No
df |2,3|5|2| No
asd |1,7,2|10|3| No
sdf |7,2,3|12|3| Yes
asdf |1,7,2,3|13|4| No
Input: word ="asdf"Output: 6Explanation: The table above contains the details about every substring of word, and we can see that 6 of them are divisible.
Example 2:
1
2
3
4
Input: word ="bdh"Output: 4Explanation: The 4 divisible substrings are:"b","d","h","bdh".It can be shown that there are no other substrings of word that are divisible.
Example 3:
1
2
3
4
Input: word ="abcd"Output: 6Explanation: The 6 divisible substrings are:"a","b","c","d","ab","cd".It can be shown that there are no other substrings of word that are divisible.
#include<vector>#include<string>usingnamespace std;
classSolution {
public:int divisibleSubstrings(string word) {
vector<int> map(26);
string digits ="12233344445556667777888999";
for (int i =0; i <26; ++i) map[i] = digits[i]-'0';
int n = word.size(), ans =0;
vector<int> pre(n+1, 0);
for (int i =0; i < n; ++i) pre[i+1] = pre[i] + map[word[i]-'a'];
for (int i =0; i < n; ++i)
for (int j = i; j < n; ++j) {
int sum = pre[j+1] - pre[i];
int len = j-i+1;
if (sum % len ==0) ++ans;
}
return ans;
}
};
classSolution {
publicintdivisibleSubstrings(String word) {
int[] map =newint[26];
String digits ="12233344445556667777888999";
for (int i = 0; i < 26; ++i) map[i]= digits.charAt(i)-'0';
int n = word.length(), ans = 0;
int[] pre =newint[n+1];
for (int i = 0; i < n; ++i) pre[i+1]= pre[i]+ map[word.charAt(i)-'a'];
for (int i = 0; i < n; ++i)
for (int j = i; j < n; ++j) {
int sum = pre[j+1]- pre[i];
int len = j-i+1;
if (sum % len == 0) ans++;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
fundivisibleSubstrings(word: String): Int {
val digits = "12233344445556667777888999"val map = IntArray(26) { digits[it]-'0' }
val n = word.length
val pre = IntArray(n+1)
for (i in0 until n) pre[i+1] = pre[i] + map[word[i]-'a']
var ans = 0for (i in0 until n)
for (j in i until n) {
val sum = pre[j+1] - pre[i]
val len = j-i+1if (sum % len ==0) ans++ }
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution:
defdivisibleSubstrings(self, word: str) -> int:
digits ="12233344445556667777888999" map = {chr(ord('a')+i): int(digits[i]) for i in range(26)}
n = len(word)
pre = [0]*(n+1)
for i in range(n):
pre[i+1] = pre[i] + map[word[i]]
ans =0for i in range(n):
for j in range(i, n):
s = pre[j+1] - pre[i]
l = j-i+1if s % l ==0:
ans +=1return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
impl Solution {
pubfndivisible_substrings(word: String) -> i32 {
let digits =b"12233344445556667777888999";
let map: Vec<i32>= digits.iter().map(|&d| (d-b'0') asi32).collect();
let n = word.len();
let word = word.as_bytes();
letmut pre =vec![0; n+1];
for i in0..n { pre[i+1] = pre[i] + map[(word[i]-b'a') asusize]; }
letmut ans =0;
for i in0..n {
for j in i..n {
let sum = pre[j+1] - pre[i];
let len = (j-i+1) asi32;
if sum % len ==0 { ans +=1; }
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
divisibleSubstrings(word: string):number {
constdigits="12233344445556667777888999";
constmap= Array.from(digits).map(d=> Number(d));
constn=word.length;
constpre= Array(n+1).fill(0);
for (leti=0; i<n; ++i) pre[i+1] =pre[i] +map[word.charCodeAt(i)-97];
letans=0;
for (leti=0; i<n; ++i)
for (letj=i; j<n; ++j) {
constsum=pre[j+1] -pre[i];
constlen=j-i+1;
if (sum%len===0) ans++;
}
returnans;
}
}