Show problems as
Classic problem #1930MediumHash TableStringBit ManipulationPrefix Sum

Unique Length-3 Palindromic Subsequences

Real-world version: Three-Letter Mirrored Picks
Tracked in this browser only
Read Unique Length-3 Palindromic Subsequences on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A tape reads s, a string of lowercase letters. A pick takes three letters from it, keeping their order, and the pick is mirrored when its first and last letters are the same.

Return how many different mirrored picks the tape holds, counting two picks as the same when they read the same.

Examples

Example 1

Input
s = "bcabcaba"
Output
8

Every one of a, b and c appears at least twice, and the distinct letters lying between the first and last place of each supply the middles, which comes to this many different picks.

Example 2

Input
s = "abc"
Output
0

No letter appears twice, so no pick can have matching ends.

Example 3

Input
s = "aaa"
Output
1

The only pick is "aaa".

Constraints

  • 3 <= s.length <= 10^5
  • s consists of lowercase English letters only

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def count_palindromic_subsequence(s: str) -> int:
Java
public int countPalindromicSubsequence(String s)