FastPrepFastPrep
Problem Brief

Number of Divisible Substrings (For MLE)

NEW GRADOA

Each character of the English alphabet has been mapped to a digit as shown below.

A string is divisible if the sum of the mapped values of its characters is divisible by its length.

Given a string s, return the number of divisible substrings of s.

A substring is a contiguous non-empty sequence of characters within a string.

1Example 1

Input
word = "asdf"
Output
6
Explanation
Example 1 illustration

The table above contains the details about every substring of word, and we can see that 6 of them are divisible.

2Example 2

Input
word = "bdh"
Output
4
Explanation

The 4 divisible substrings are: "b", "d", "h", "bdh".
It can be shown that there are no other substrings of word that are divisible.

3Example 3

Input
word = "abcd"
Output
6
Explanation

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.

Constraints

Limits and guarantees your solution can rely on.

  • 1 <= word.length <= 2000
  • word consists only of lowercase English letters.
public int numberOfDivisibleSubstrings(String word) {
  // write your code here (You might want to refer to LC 2950 :)
}
Input

word

"asdf"

Output

6

Sign in to submit your solution.