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.

Example 2:

Input:
S = 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba'
Output: 104860361
Explanation:
There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 10^9 + 7.
 class Solution {
public int countPalindromicSubsequences(String S) {
Map<String, Set<String>> _seqMap = new HashMap<>();
return findPalindromesHelper(S, _seqMap).size();
} private Set<String> findPalindromesHelper(String s, Map<String, Set<String>> _seqMap) {
Set<String> result = _seqMap.get(s);
if (result != null) {
return result;
}
int len = s.length();
result = new HashSet<String>();
if (len < ) {
return result;
}
if (len == ) {
result.add(s);
return result;
}
result.addAll(findPalindromesHelper(s.substring(, len - ), _seqMap));
result.addAll(findPalindromesHelper(s.substring(, len), _seqMap));
if (s.charAt() == s.charAt(len - )) {
Set<String> subSet = findPalindromesHelper(s.substring(, len - ), _seqMap);
String head = s.substring(, );
for (String s1 : subSet) {
result.add(head + s1 + head);
}
result.add(head + head);
}
_seqMap.put(s, result);
return result;
}
}
 class Solution {
public int countPalindromicSubsequences(String s) {
int len = s.length();
int[][] dp = new int[len][len]; char[] chs = s.toCharArray();
for (int i = ; i < len; i++) {
dp[i][i] = ; // Consider the test case "a", "b" "c"...
} for (int distance = ; distance < len; distance++) {
for (int i = ; i < len - distance; i++) {
int j = i + distance;
if (chs[i] == chs[j]) {
int low = i + , high = j - ;
while (low <= high && chs[low] != chs[j]) {
low++;
}
while (low <= high && chs[high] != chs[j]) {
high--;
}
if (low > high) {
/*
* consider the string from i to j is "a...a" "a...a"... where there is no
* character 'a' inside the leftmost and rightmost 'a'
*
* eg: "aba" while i = 0 and j = 2: dp[1][1] = 1 records the palindrome{"b"},
* the reason why dp[i + 1][j - 1] * 2 counted is that we count dp[i + 1][j - 1]
* one time as {"b"}, and additional time as {"aba"}. The reason why 2 counted
* is that we also count {"a", "aa"}. So totally dp[i][j] record the palindrome:
* {"a", "b", "aa", "aba"}.
*/
dp[i][j] = dp[i + ][j - ] * + ;
} else if (low == high) {
/*
* consider the string from i to j is "a...a...a" where there is only one
* character 'a' inside the leftmost and rightmost 'a'
*
* eg: "aaa" while i = 0 and j = 2: the dp[i + 1][j - 1] records the palindrome
* {"a"}. the reason why dp[i + 1][j - 1] * 2 counted is that we count dp[i +
* 1][j - 1] one time as {"a"}, and additional time as {"aaa"}. the reason why 1
* counted is that we also count {"aa"} that the first 'a' come from index i and
* the second come from index j. So totally dp[i][j] records {"a", "aa", "aaa"}
*/
dp[i][j] = dp[i + ][j - ] * + ;
} else {
/*
* consider the string from i to j is "a...a...a... a" where there are at least
* two character 'a' close to leftmost and rightmost 'a'
*
* eg: "aacaa" while i = 0 and j = 4: the dp[i + 1][j - 1] records the
* palindrome {"a", "c", "aa", "aca"}. the reason why dp[i + 1][j - 1] * 2
* counted is that we count dp[i + 1][j - 1] one time as {"a", "c", "aa",
* "aca"}, and additional time as {"aaa", "aca", "aaaa", "aacaa"}. Now there is
* duplicate : {"aca"}, which is removed by deduce dp[low + 1][high - 1]. So
* totally dp[i][j] record {"a", "c", "aa", "aca", "aaa", "aaaa", "aacaa"}
*/
dp[i][j] = dp[i + ][j - ] * - dp[low + ][high - ];
}
} else {
dp[i][j] = dp[i][j - ] + dp[i + ][j] - dp[i + ][j - ]; // s.charAt(i) != s.charAt(j)
}
dp[i][j] = dp[i][j] < ? dp[i][j] + : dp[i][j] % ;
}
}
return dp[][len - ];
}
}

Count Different Palindromic Subsequences的更多相关文章

  1. leetcode 730 Count Different Palindromic Subsequences

    题目链接: https://leetcode.com/problems/count-different-palindromic-subsequences/description/ 730.Count ...

  2. LN : leetcode 730 Count Different Palindromic Subsequences

    lc 730 Count Different Palindromic Subsequences 730 Count Different Palindromic Subsequences Given a ...

  3. [Swift]LeetCode730. 统计不同回文子字符串 | Count Different Palindromic Subsequences

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  4. 730. Count Different Palindromic Subsequences

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  5. 【LeetCode】730. Count Different Palindromic Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化搜索 动态规划 日期 题目地址:https:/ ...

  6. [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  7. [LeetCode] 730. Count Different Palindromic Subsequences 计数不同的回文子序列的个数

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. Debug .NET Framework Source

    1.Microsoft Reference Source 在线source http://referencesource.microsoft.com/#System.Data.Linq 可以下载离线s ...

  2. 期望与概率dp

    概率与期望dp 定义: 概率:事件A发生的可能性,计作P(A) 期望:事件A结果的平均大小,记住E(x) ​ E(x)=每种结果的大小与其概率的乘积的和 注意计算概率时需要考虑是否要用容斥原理 期望d ...

  3. 【luoguP3959 宝藏】-状压DP

    题目描述: 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 n 个深埋在地下的宝藏屋, 也给出了这 n 个宝藏屋之间可供开发的m 条道路和它们的长度. 小明决心亲自前往挖掘所有宝藏屋中的宝藏.但是 ...

  4. python 正则相关函数全解析

    前言:网上有很多关于python正则函数的方法说明,这里尽可能用最简单的demo把所有函数之间的逻辑关系说清楚,供参考. 1.最原始的 re.compile()这个函数一般是需要和其它函数一起使用的, ...

  5. nodejs的npm命令无反应的解决方案

    这二天用npm下载模块的时候输入npm命令完全无反应,不是加载的那种状态而是下标不停地在哪里闪...之后找解决方案,说要删除npmrc文件.强调:不是nodejs安装目录npm模块下的那个npmrc文 ...

  6. 牛券Cow Coupons

    USACO12FEB 久违的奶牛题. 题意: FJ准备买一些新奶牛,市场上有 $ N $ 头奶牛 $ (1 \leq N \leq 50000) $ ,第i头奶牛价格为 $ P_i (1 \leq P ...

  7. 【sed】基本用法

    1. 文本处理 sed编辑器根据sed命令处理数据流中的数据:在流编辑器将所有命令与一行数据匹配完后,它会读取下一行数据并重复以下过程: (1) 一次从输入中读取一行数据 (2) 根据所提供的编辑器命 ...

  8. Google 插件的使用

    每次看英文网页或者文档的时候总会碰到不认识单词,就想能不能选中单词就可以显示翻译?于是就安装Google浏览器的翻译插件,总的来说,蛮繁琐的. 1.先安装谷歌访问助手 (1.)直接百度谷歌访问助手 ( ...

  9. HearthBuddy炉石兄弟 如何调试ai

    Sepefeets's update to botmaker's Silverfish AI This AI is a Custom Class for Hearthranger and Hearth ...

  10. useMemo、useCallback简单理解

    1.useMemo.useCallback都是使参数(函数)不会因为其他不想关的参数变化而重新渲染. (1)useMemo const memoDom = useMemo(() => { ret ...