Back to DSA sheet

Unique Length 3 Palindromic Subsequences

MediumArrays

Count the number of distinct length-3 palindromic subsequences in a string. A length-3 palindrome looks like x?x: the same character on both ends with any single character in the middle.

Input format (stdin): a single line, the string. Output the count of distinct such palindromes.

Examples
Input: aabca
Output: 3
aba, aaa, aca are the distinct palindromes.
Input: adc
Output: 0
No character repeats with something between.
Constraints
  • 3 <= length <= 10^5
  • lowercase English letters
Sheets
NeetCode 250
unique-length-3-palindromic-subsequences.cpp3 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.