1

Source Leetcode

Given a string S, find the number of different non-empty palindromic subsequences in S, and return that number modulo 10^9 + 7.

A subsequence of a string S is obtained by deleting 0 or more characters from S.

A sequence is palindromic if it is equal to the sequence reversed.

Two sequences A_1, A_2, ... and B_1, B_2, ... are different if there is some i for which A_i != B_i.

Example 1: Input: S = 'bccb'

Output: 6

Explanation: The 6 different non-empty palindromic subsequences are 'b', 'c', 'bb', 'cc', 'bcb', 'bccb'.

Note that 'bcb' is counted only once, even though it occurs twice.

Sol 1. Given Solution here, it looks quite unintuitive

Sol 2. The Other solution which I do understand is to find all palindromic subsequence and use set to keep it distinct but output can be very large and keeping them in the set is not good Idea I think.(here)

Is there any better and intuitive approach to solve this problem?

Atiq
  • 490
  • 11
  • 28
  • Looks like a duplicate of [this](https://stackoverflow.com/questions/28853952/total-number-of-palindromic-subsequences-in-a-string?rq=1) – Raghu Kumar Apr 07 '18 at 06:04

0 Answers0