All problems
0962MediumHash TableStringBit ManipulationPrefix Sum

Three-Letter Mirrored Picks

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1930Unique Length-3 Palindromic Subsequences

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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