All problems
1151MediumHash TableStringCountingPrefix Sum

Stretches Whose Total Divides Their Length

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2950Number of Divisible Substrings

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

Every lowercase letter carries a number. The first three letters carry 1, the next three carry 2, and so on in groups of three, so the last group, holding only y and z, carries 9.

A stretch of neighbouring letters is even-handed when the total of its letters' numbers divides exactly by how many letters the stretch holds.

Return how many even-handed stretches the word word holds.

Examples

Example 1

Input
word = "az"
Output
3

Each letter on its own is even-handed, and together they total 10 across two letters, which divides evenly.

Example 2

Input
word = "zzz"
Output
6

Every letter carries 9, so every stretch totals nine times its own length.

Example 3

Input
word = "abz"
Output
5

The three single letters count, and so do the first two together totalling 2 and the last two totalling 10. All three together total 11, which three does not divide.

Constraints

  • 1 <= word.length <= 2000
  • word holds only lowercase English letters

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def count_divisible_substrings(word: str) -> int:
Java
public int countDivisibleSubstrings(String word)
September 7
Apply