Leetcode Problem 1930. Unique Length-3 Palindromic Subsequences

1930. Unique Length-3 Palindromic Subsequences

Leetcode Solutions

Count Unique Palindromic Subsequences

  1. Create a hash set letters to store all unique characters in s.
  2. Initialize ans to 0, which will hold the count of unique palindromic subsequences.
  3. For each letter in letters: a. Find the first occurrence index i and the last occurrence index j in s. b. Initialize a hash set between. c. Iterate over the indices k between i and j (exclusive) and add s[k] to between. d. Add the size of between to ans.
  4. Return ans.
UML Thumbnail

Pre-Compute First and Last Indices for Palindromic Subsequences

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...