Ctrl K

Pieces of the Alphabet Ring

Medium
Write the Latin alphabet around a circle so that a follows z, and repeat it forever in both directions: ...xyzabcd...xyzabc.... You are given a string text of lowercase Latin letters. Count how many **distinct** non-empty substrings of text appear in that endless sequence, meaning each letter is the one right after the previous letter in the alphabet (with z wrapping to a). Equal substrings found at different places in text are counted once.
Example 1
text='xyzab' → 15

The whole string follows the alphabet ring: x→y→z→a→b. Every substring fits, and there are 5 + 4 + 3 + 2 + 1 = 15 of them, all different.

Example 2
text='abab' → 3

"a", "b" and "ab" fit. "ba" does not, since b is followed by c. Repeats count once, so the answer is 3.

Example 3
text='dog' → 3
Constraints
  • 1 ≤ text.length ≤ 10⁵
  • text consists of lowercase Latin letters only
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Mirror Fragment MediumAll Balanced Bracket Strings MediumFewest Letter Edits MediumWays to Read a Digit Code MediumWeaving Two Strings Medium
loading Python…
Input
text='xyzab'
Expected
15

Run checks the first 3 tests in your browser. Submit checks all 69 tests on the server, hidden ones included.