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:
7
💡 Note:
Character values: a=1, s=1, d=4, f=6. Divisible substrings: "a"(1÷1=0), "s"(1÷1=0), "d"(4÷1=0), "f"(6÷1=0), "as"(2÷2=0), "df"(10÷2=0), "asd"(6÷3=0), "asdf"(12÷4=0). Non-divisible: "sd"(5÷2≠0), "sdf"(11÷3≠0). Total divisible: 7.
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:
2
💡 Note:
Substrings: "a"(1÷1=0), "b"(2÷1=0), "ab"(3÷2≠0). Only "a" and "b" are divisible. Total = 2.
Constraints
- 1 ≤ s.length ≤ 1000
- s consists of lowercase English letters only
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code