Skip to content

Commit da9c7d9

Browse files
authored
Update count-different-palindromic-subsequences.py
1 parent 155da7d commit da9c7d9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Python/count-different-palindromic-subsequences.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
11
# Time: O(n^2)
22
# Space: O(n^2)
33

4+
# Given a string S, find the number of different non-empty palindromic subsequences in S,
5+
# and return that number modulo 10^9 + 7.
6+
#
7+
# A subsequence of a string S is obtained by deleting 0 or more characters from S.
8+
#
9+
# A sequence is palindromic if it is equal to the sequence reversed.
10+
#
11+
# Two sequences A_1, A_2, ... and B_1, B_2, ... are different if there is some i for which A_i != B_i.
12+
#
13+
# Example 1:
14+
# Input:
15+
# S = 'bccb'
16+
# Output: 6
17+
# Explanation:
18+
# The 6 different non-empty palindromic subsequences are 'b', 'c', 'bb', 'cc', 'bcb', 'bccb'.
19+
# Note that 'bcb' is counted only once, even though it occurs twice.
20+
#
21+
# Example 2:
22+
# Input:
23+
# S = 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba'
24+
# Output: 104860361
25+
#
26+
# Explanation:
27+
# There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 10^9 + 7.
28+
# Note:
29+
# - The length of S will be in the range [1, 1000].
30+
# - Each character S[i] will be in the set {'a', 'b', 'c', 'd'}.
31+
432
class Solution(object):
533
def countPalindromicSubsequences(self, S):
634
"""

0 commit comments

Comments
 (0)