730. Count Different Palindromic Subsequences
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, ...
andB_1, B_2, ...
are different if there is somei
for whichA_i != B_
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.
Note:
- The length of
S
will be in the range[1, 1000]
.- Each character
S[i]
will be in the set{'a', 'b', 'c', 'd'}
.
Approach #1: DFS + Memeory. [C++][MLE]
class Solution {
public:
int countPalindromicSubsequences(string S) {
return count(S);
} private:
unordered_map<string, long> memo;
static constexpr long mod = 1000000007; int count(const string& s) {
if (s.empty()) return 0;
if (s.length() == 1) return 1;
if (memo[s] > 0) return memo[s];
int len = s.length();
long ans = 0;
if (s[0] == s[len-1]) {
int l = 1, r = len - 2;
while (l <= r && s[l] != s[0]) l++;
while (l <= r && s[r] != s[len-1]) r--;
if (l > r) ans = count(s.substr(1, len-2))*2 + 2;
else if (l == r) ans = count(s.substr(1, len-2))*2 + 1;
else ans = count(s.substr(1, len-2))*2 - count(s.substr(l+1, r-l-1));
} else {
ans = count(s.substr(0, len-1)) + count(s.substr(1, len-1)) - count(s.substr(1, len-2));
} ans = (ans + mod) % mod;
// cout << ans << endl; return memo[s] = ans;
}
};
Approach #2: Optimization. [C++]
class Solution {
public:
int countPalindromicSubsequences(string S) {
int len = S.length();
memo = vector<int>(len*(len+1)+1, 0);
return count(S, 0, len-1);
} private:
vector<int> memo;
static constexpr long mod = 1000000007; int count(const string& S, int s, int e) {
if (s > e) return 0;
if (s == e) return 1;
int key = s * S.length() + e;
if (memo[key] > 0) return memo[key];
int len = S.length();
long ans = 0;
if (S[s] == S[e]) {
int l = s+1, r = e-1;
while (l <= r && S[l] != S[s]) l++;
while (l <= r && S[r] != S[e]) r--;
if (l > r) ans = count(S, s+1, e-1)*2 + 2;
else if (l == r) ans = count(S, s+1, e-1)*2 + 1;
else ans = count(S, s+1, e-1)*2 - count(S, l+1, r-1);
} else {
ans = count(S, s+1, e) + count(S, s, e-1)
- count(S, s+1, e-1);
} return memo[key] = (ans + mod) % mod;
}
};
Approach #3: DP. [C++]
class Solution {
long mod = 1000000007;
public int countPalindromicSubsequences(String S) {
int len = S.length();
long[][] dp = new long[len][len];
for (int i = 0; i < len; ++i)
dp[i][i] = 1;
for (int k = 1; k <= len; ++k) {
for (int i = 0; i < len-k; ++i) {
int j = i + k;
if (S.charAt(i) == S.charAt(j)) {
dp[i][j] = dp[i+1][j-1] * 2;
int l = i + 1;
int r = j - 1;
while (l <= r && S.charAt(l) != S.charAt(i)) l++;
while (l <= r && S.charAt(r) != S.charAt(j)) r--;
if (l > r) dp[i][j] += 2;
else if (l == r) dp[i][j] += 1;
else dp[i][j] -= dp[l+1][r-1];
} else
dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1];
dp[i][j] = (dp[i][j] + mod) % mod;
}
}
return (int)dp[0][len-1];
}
}
Reference:
730. Count Different Palindromic Subsequences的更多相关文章
- leetcode 730 Count Different Palindromic Subsequences
题目链接: https://leetcode.com/problems/count-different-palindromic-subsequences/description/ 730.Count ...
- LN : leetcode 730 Count Different Palindromic Subsequences
lc 730 Count Different Palindromic Subsequences 730 Count Different Palindromic Subsequences Given a ...
- [LeetCode] 730. Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- 【LeetCode】730. Count Different Palindromic Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化搜索 动态规划 日期 题目地址:https:/ ...
- [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- [Swift]LeetCode730. 统计不同回文子字符串 | Count Different Palindromic Subsequences
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- Count Different Palindromic Subsequences
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
随机推荐
- sqlserver查询区分大小写
例子: select * from tb_students where name='jay' select * from tb_students where name='JAY' 这两句查询结果是一样 ...
- POJ 1122.FDNY to the Rescue! Dijkstra
FDNY to the Rescue! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2808 Accepted: 86 ...
- 移动端UI设计规范模板参考以及设计规范的好处
2018也快要过完了(-_-),我们的移动端的UI设计规范也层出不穷.很多APP设计师也要在年底给公司或者是团队做一个总结.那么一个像样的APP ui设计规范也是很有必要的作品回顾. 在创业公司做着一 ...
- Tomcat的杂七杂八
localhost_access_log.2016-01-15.txt 原来这里面有访问记录. /logs/catalina.2016-01-22.log 这里有显示失败的信息 2016-01-23 ...
- 2018.07.04 POJ 1696 Space Ant(凸包卷包裹)
Space Ant Time Limit: 1000MS Memory Limit: 10000K Description The most exciting space discovery occu ...
- 前端学习—React—初出茅庐
React学习—初出茅庐 对与React的学习思路,首先React中用到了Class.let.const.以及modual(模块)的导入(import)和导出(export),而这些都是ECMAScr ...
- Django入门与实践-第19章:主题回复(完结)
http://127.0.0.1:8000/boards/1/topics/1/reply/ http://127.0.0.1:8000/boards/1/topics/1/ #myproject/u ...
- n&&m and n||m 的区别
今天写一道题老是WA最后才发现问题出在了这个地方, 题目说的是当输入的n和m 都为0的时候,结束输入. 于是乎,条件我就写成了while(n&&m),其实这句话的意思是:只有m和n都不 ...
- 搜狗Q3业绩迅猛增长,战略整合稳步推进
继9月16日腾讯与搜狗战略结盟之后,最近搜狗再次吸引了业界关注的目光,10月29日,搜狗公布了截至2013年9月30日的第三季度未经审计的财务报告.财报显示,新搜狗Q3营收达5700万美元,同 ...
- 讲解java异常
J2EE项目异常处理 为什么要在J2EE项目中谈异常处理呢?可能许多java初学者都想说:“异常处理不就是try….catch…finally吗?这谁都会 ...