Count Five-Position Palindromes in a Digit String

Quick Overview

Count five-position palindromic subsequences in a digit string, counting different index selections separately and reducing modulo 1,000,000,007.

Count Five-Position Palindromes in a Digit String

Company: Visa

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Count subsequences of digit string `s` with exactly five characters that form a palindrome. Different increasing choices of indices count separately, even when their resulting strings are identical. Return the count modulo `10^9 + 7`. ```cpp class Solution { public: int countPalindromes(string s); }; ``` ### Examples **Example 1:** ```text Input: s = "111111" Output: 6 Explanation: Omitting any one of the six positions gives one of six index-distinct palindromic subsequences. ``` **Example 2:** ```text Input: s = "12345" Output: 0 Explanation: The only length-five subsequence is not a palindrome. ``` ### Constraints - `1 <= s.length <= 10000`. - s consists of decimal digits. ### Clarifying Questions - Must the five positions be consecutive? No; they form a subsequence. - Do equal strings from different indices count separately? Yes. - Is the length exactly five? Yes. - What if the input has fewer than five characters? Return 0. ```hint Check the boundary Distinguish the number of different strings from the number of different five-index choices. ```

Overview: Count five-position palindromic subsequences in a digit string, counting different index selections separately and reducing modulo 1,000,000,007.

Visa logo
Visa
Source window: 0–6 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Count subsequences of digit string s with exactly five characters that form a palindrome. Different increasing choices of indices count separately, even when their resulting strings are identical. Return the count modulo 10^9 + 7.

class Solution {
public:
    int countPalindromes(string s);
};

Examples

Example 1:

Input: s = "111111"
Output: 6
Explanation: Omitting any one of the six positions gives one of six index-distinct palindromic subsequences.

Example 2:

Input: s = "12345"
Output: 0
Explanation: The only length-five subsequence is not a palindrome.

Constraints

  • 1 <= s.length <= 10000 .
  • s consists of decimal digits.

Clarifying Questions Guidance

  • Must the five positions be consecutive? No; they form a subsequence.
  • Do equal strings from different indices count separately? Yes.
  • Is the length exactly five? Yes.
  • What if the input has fewer than five characters? Return 0.