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. PHP mysqli_kill() 函数

    定义和用法 mysqli_kill() 函数请求服务器杀死一个由 processid 参数指定的 MySQL 线程. 语法 mysqli_kill(connection,processid);   实 ...

  2. 【csp模拟赛1】不服来战 (challenge.cpp)

    [题目描述] 最近小 Z 和他的朋友都迷上了一款手机游戏:不服来战. 游戏的设定十分简单,在游戏开始时,会给出一排共 N 个灯,有的灯是开着 的有的是关着的,每个灯都有一个分数.而玩家可以进行任意次操 ...

  3. ACM之路(17)—— 博弈论

    博弈论这方面网上资料庞大,我觉得我不可能写的比他们好,就转载一下我觉得写的不错的博客好了. 首先是三大博弈:巴什博奕,威佐夫博奕,尼姆博奕.博客:三大基本博弈. 然后是强大的sg函数和sg定理:SG. ...

  4. javascript中的BOM

    浏览器对象模型BOM,提供了访问浏览器的接口.这些功能大多和网页内容无关,多年来,由于缺乏规范导致BOM中的不同方法在不同浏览器中的实现有所差异,直到html5,才将BOM的主要方面纳入规范. BOM ...

  5. Leetcode题目206.反转链表(简单)

    题目描述: 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 进阶: ...

  6. 工具类注入需要的service

    /** * 从redis获取信息 * @author yy * */ @Component//关键一:添加此注解才能被spring扫描到 public class CacheUtil { privat ...

  7. PHP环境搭建之单独安装

    还在使用PHP集成环境吗?教你自定义搭建配置PHP开发环境,按照需求进行安装,安装的版本可以自己选择,灵活性更大. 目录:1. 安装Apache2. 安装PHP3. 安装MySQL4. 安装Compo ...

  8. 配置默认编码为utf8

    修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示: [mysqld] character_set_server=utf8 init_connect='SET NAMES ...

  9. 将蓝牙rssi(信号强度)转换成距离

    遇到一个问题,是将蓝牙rssi(信号强度)转换成距离的问题. 这一问题没有准确的解决办法,但是有人做过一个拟合回归函数,其变化规律比较类似于rssi的变化规律,函数如下: d = ^(abs(rssi ...

  10. 1.ibatis核心类