844. Five-Letter Mirrors

A safe uses a keypad code stored as the digit string digits. A technician says a code is a mirror if it has exactly five digits and reads the same from both ends, like 4 7 0 7 4. She wants to know how many mirror codes hide inside the stored string as subsequences, i.e. picking five positions from left to right without reordering.

Count position choices, not distinct codes, so two selections with different positions are counted separately even if they spell the same digits. Return the count modulo 1000000007. Strings shorter than five digits give 0.

Aim for time O(100 * n) and extra space O(100).

Example 1

Input:
digits = "2727272"
Output:
9
Explanation:

Out of the 21 ways to choose five positions, 9 spell a palindrome, for example the first five digits 27272.

Example 2

Input:
digits = "123321456"
Output:
2
Explanation:

Only 12321 qualifies, built with either of the two middle 3s, giving 2 subsequences.

Constraints

1 ≤ digits.length ≤ 105

digits consists only of the characters 0 to 9

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(100 * n)
Space
O(100)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…