Number of Divisible Substrings - Problem
Each character of the English alphabet has been mapped to a digit as shown below:
a → 1, b → 2, c → 3, d → 4, e → 5, f → 6, g → 7, h → 8, i → 9, j → 1, k → 2, l → 3, m → 4, n → 5, o → 6, p → 7, q → 8, r → 9, s → 1, t → 2, u → 3, v → 4, w → 5, x → 6, y → 7, z → 8
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.
Input & Output
Example 1 — Simple String
$
Input:
s = "asdf"
›
Output:
10
💡 Note:
Character values: a=1, s=1, d=4, f=6. All substrings: "a"(1÷1), "s"(1÷1), "d"(4÷1), "f"(6÷1), "as"(2÷2), "sd"(5÷2), "df"(10÷2), "asd"(6÷3), "sdf"(11÷3), "asdf"(12÷4). Count divisible ones: 10 total.
Example 2 — Single Character
$
Input:
s = "a"
›
Output:
1
💡 Note:
Only one substring "a" with value 1 and length 1. Since 1 % 1 = 0, it's divisible.
Example 3 — Two Characters
$
Input:
s = "ab"
›
Output:
3
💡 Note:
Substrings: "a"(1÷1=0), "b"(2÷1=0), "ab"(3÷2=1.5). Only "a" and "b" are divisible, plus single chars always divisible, total = 3.
Constraints
- 1 ≤ s.length ≤ 1000
- s consists of lowercase English letters only
Visualization
Tap to expand
Understanding the Visualization
1
Input
String with character-to-digit mapping
2
Process
Check all substrings for sum % length == 0
3
Output
Count of divisible substrings
Key Takeaway
🎯 Key Insight: Use modular arithmetic to efficiently check if substring sum is divisible by its length
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code