|
2 | 2 |
|
3 | 3 | import java.util.Arrays;
|
4 | 4 |
|
5 | | -/** Created by gouthamvidyapradhan on 10/04/2021 */ |
| 5 | +/** |
| 6 | + * Created by gouthamvidyapradhan on 10/04/2021 Given a string S, find the number of different |
| 7 | + * non-empty palindromic subsequences in S, and return that number modulo 10^9 + 7. |
| 8 | + * |
| 9 | + * <p>A subsequence of a string S is obtained by deleting 0 or more characters from S. |
| 10 | + * |
| 11 | + * <p>A sequence is palindromic if it is equal to the sequence reversed. |
| 12 | + * |
| 13 | + * <p>Two sequences A_1, A_2, ... and B_1, B_2, ... are different if there is some i for which A_i |
| 14 | + * != B_i. |
| 15 | + * |
| 16 | + * <p>Example 1: Input: S = 'bccb' Output: 6 Explanation: The 6 different non-empty palindromic |
| 17 | + * subsequences are 'b', 'c', 'bb', 'cc', 'bcb', 'bccb'. Note that 'bcb' is counted only once, even |
| 18 | + * though it occurs twice. Example 2: Input: S = |
| 19 | + * 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba' Output: 104860361 Explanation: |
| 20 | + * There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 10^9 |
| 21 | + * + 7. Note: |
| 22 | + * |
| 23 | + * <p>The length of S will be in the range [1, 1000]. Each character S[i] will be in the set {'a', |
| 24 | + * 'b', 'c', 'd'}. |
| 25 | + * |
| 26 | + * Solution: O(N ^ 2) x 4 |
| 27 | + */ |
6 | 28 | public class CountDifferentPalindromicSubsequences {
|
7 | 29 | public static void main(String[] args) {
|
8 | 30 | System.out.println(
|
|
0 commit comments