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 ...
随机推荐
- loadrunner--TPS和平均事务响应时间
TPS就是每秒事务数,但是事务是基于虚拟用户数的,假如1个虚拟用户在1秒内完成1笔事务,那么TPS明显就是1:如果 某笔业务响应时间是1ms,那么1个用户在1秒内能完成1000笔事务,TPS就是100 ...
- 抓取访客ip
$realip = $_SERVER['REMOTE_ADDR'] $proxy_ip = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);//有用到代理 ...
- FSCapture截图软件注册码
企业版序列号: name:bluman serial/序列号/注册码:VPISCJULXUFGDDXYAUYF
- eclipse安装automake
help->Install new software
- zeromq学习记录(二)天气更新服务器使用ZMQ_SUB ZMQ_PUB
/************************************************************** 技术博客 http://www.cnblogs.com/itdef/ ...
- 2018.08.04 bzoj3261: 最大异或和(trie)
传送门 简单可持久化01trie树. 实际上这东西跟可持久化线段树貌似是一个东西啊. 要维护题目给出的信息,就需要维护前缀异或和并且把它们插入一棵01trie树,然后利用贪心的思想在上面递归就行了,因 ...
- 8.7 使用索引-notes
七 正确使用索引 一 索引未命中 并不是说我们创建了索引就一定会加快查询速度,若想利用索引达到预想的提高查询速度的效果,我们在添加索引时,必须遵循以下问题 1 范围问题,或者说条件不明确,条件中出现这 ...
- s111 stark组件
内容回顾: 1. 类当做key 2. django中的model所在app名称.以及小写类名. def index(request): # print(m1.UserInfo,m1.UserInfo. ...
- python编码(四)
一.预备知识 字符集 1, 常用字符集分类 ASCII及其扩展字符集作用:表语英语及西欧语言.位数:ASCII是用7位表示的,能表示128个字符:其扩展使用8位表示,表示256个字符.范围:ASCII ...
- java thrift返回List异常
对于下段代码: public List<String> hmget(String key, List<String> fields) throws org.apache.thr ...