730. Count Different Palindromic Subsequences

09/19/2021

Leetcodearrow-up-right

Given a string s, return the number of different non-empty palindromic subsequences in s. Since the answer may be very large, return it modulo 109 + 7.

A subsequence of a string is obtained by deleting zero or more characters from the string.

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

Two sequences a1, a2, ... and b1, b2, ... are different if there is some i for which ai != bi.

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.

Example 2:

Input: s = "abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba"
Output: 104860361
Explanation: There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 109 + 7.

Constraints:

  • 1 <= s.length <= 1000

  • s[i] is either 'a', 'b', 'c', or 'd'.

Basic Idea

General的DP思路比较复杂,也有太多的edge case需要考虑,可以看花花酱的视频,但我个人觉得在面试中写出来难度太大,所以这里来记录另一种DP思路。

考虑到所给的字母范围是比较小的,只有 abcd,不难想到利用有限的字母以及他们的index来做文章。这种思路就是从外层先开始考虑,每次考虑一种字母,例如最外层考虑a...a, b....b, c.....c, d...d的情况,然后例如对于 a....a下一层考虑 a...a...a...a, a...b...b...a ... 的情况。例如对于题目中的例子 bccb ,最外层的循环考虑 bb有2种(b, bb),cc有两种(c, cc),然后对于b...b的情况,下一层考虑了(bccb)和(bcb)共有两种,一共6种。

Java Code:

Last updated