Skip to content

1930. Unique Length-3 Palindromic Subsequences #1071

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We can use an efficient algorithm that leverages prefix and suffix character tracking to count all valid palindromic subsequences.

Approach

  1. Track Prefix Characters:
    Use an array to store the set of characters encountered to the left of each position in the string. This will help in efficiently checking if a character can form the first part of a palindromic subsequence.

  2. Track Suffix Characters:
    Use another array to store the set of characters encountered to the right of each position in the string. This will help in efficiently checking if a character can form the third part of a palindromic subsequence.

  3. Count Palindromic Subsequences:
    For each character in the string, consider it a…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Jan 4, 2025
Maintainer Author

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Jan 4, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants