Unique Length-3 Palindromic Subsequences

Given a string s, return the number of unique palindromes of length three that are a subsequence of s.

Note that even if there are multiple ways to obtain the same subsequence, it is still only counted once.

A palindrome is a string that reads the same forwards and backwards.

A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.

  • For example, "ace" is a subsequence of "abcde".
Example 1
Inputs = "aabca"
Output3
The 3 palindromic subsequences of length 3 are "aba", "aaa", and "aca".
Example 2
Inputs = "adc"
Output0
There are no palindromic subsequences of length 3 in "adc".

Constraints

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

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate