Skip to main content
LeetCode 1930, Medium. Topics: Hash Table, String, Bit Manipulation, Prefix Sum. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 40 parametrized pytest cases, and a playground notebook:

Problem

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".

Examples

Constraints

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

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026