Skip to the code
+50 XP

#611 Count Mirrored Snippets at Scale

hard

The word game from before now scores whole paragraphs, up to 200,000 characters long, so counting from every center one step at a time is too slow.

Write countAllMirrors(text) that returns how many substrings of text read the same forward and backward. Substrings at different positions count separately, and single letters count.

Example 1

Input: text = "aaa"
Output: 6

Three single letters, two aa, and one aaa.

Example 2

Input: text = "abc"
Output: 3

Only the three single letters.

Constraints

  • 0 <= text.length <= 2 * 10^5
  • text contains lowercase letters.
  • The answer fits in a JavaScript number.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).