Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode 0115 - Distinct Subsequences

LeetCode 0115 - Distinct Subsequences

作者头像
Reck Zhang
发布于 2021-08-11 02:54:37
发布于 2021-08-11 02:54:37
46500
代码可运行
举报
文章被收录于专栏:Reck ZhangReck Zhang
运行总次数:0
代码可运行

Distinct Subsequences

Desicription

Given a string S and a string T, count the number of distinct subsequences of S which equals T.

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).

Here is an example: S = "rabbbit", T = "rabbit"

Return 3.

Solution

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
public:
    int numDistinct(string s, string t) {
        vector<vector<int>> dp(t.size() + 1, vector<int>(s.size() + 1, 0));
        for(int i = 0; i <= s.size(); i++)
            dp[0][i] = 1;
        for(int i = 1; i <= t.size(); i++) {
            for(int j = 1; j <= s.size(); j++) {
                if(t[i-1] == s[j-1])
                    dp[i][j] = dp[i-1][j-1] + dp[i][j-1];
                else
                    dp[i][j] = dp[i][j-1];
            }
        }
        return dp[t.size()][s.size()];
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-12-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Leetcode 115 Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative posi
triplebee
2018/01/12
7630
LeetCode 115. Distinct Subsequences题目分析代码
给出字符串S和字符串T,计算S的不同的子序列中T出现的个数。 子序列字符串是原始字符串通过删除一些(或零个)产生的一个新的字符串,并且对剩下的字符的相对位置没有影响。(比如,“ACE”是“ABCDE”的子序列字符串,而“AEC”不是)。 样例 给出S = "rabbbit", T = "rabbit" 返回 3
desperate633
2018/08/22
5250
LeetCode: Distinct Subsequences [115]
Given a string S and a string T, count the number of distinct subsequences of T in S.
全栈程序员站长
2022/07/05
4250
​LeetCode刷题实战115:不同的子序列
https://leetcode-cn.com/problems/distinct-subsequences/
程序员小猿
2021/01/19
4460
​LeetCode刷题实战115:不同的子序列
LeetCode 115 Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of S which equals T.
ShenduCC
2018/07/24
6840
LeetCode 0392 - Is Subsequence
Given a string s and a string t, check if s is subsequence of t.
Reck Zhang
2021/08/11
2280
LeetCode 115. 不同的子序列(DP)
一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串。(例如,“ACE” 是 “ABCDE” 的一个子序列,而 “AEC” 不是)
Michael阿明
2021/02/19
3670
每日算法系列【LeetCode 115】不同的子序列
一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串。(例如,"ACE" 是 "ABCDE" 的一个子序列,而 "AEC" 不是)
godweiyang
2020/03/24
9670
LeetCode笔记:392. Is Subsequence
这道题最直接的思路就是遍历t,一个个按顺序检查s中的字符是否顺序出现了,如果一直到s的最后一个字符都出现了,而且是符合顺序的,那就返回true,否则返回false。
Cloudox
2021/11/23
1820
Subsequence - 392. Is Subsequence
Given a string s and a string t, check if s is subsequence of t.
ppxai
2020/09/23
3020
[Leetcode][python]Distinct Subsequences/不同子序列
同样你可以打印出dp看结构:上半区都为0,因为不可能,dp[0][0]为1因为空转空有一种可能(不删除)
蛮三刀酱
2019/03/26
6210
leetcode392. Is Subsequence
如何判断字符串s是否是字符串t的一个子序列。子序列是指s中的字母均按照相对位置存在于t中,比如"abc"是"ahbfdc"的一个子序列,但是"axc"就不是"ahbgdc"的一个子序列。
眯眯眼的猫头鹰
2019/03/13
3200
动态规划:判断子序列
题目链接:https://leetcode-cn.com/problems/is-subsequence/
代码随想录
2021/04/07
6980
动态规划:判断子序列
力扣每日一刷(2023.9.21)
本题其实不使用动态规划的思路也是能够解出来的 ,并且时间复杂度 和 空间复杂度更低。 因为题目中问的是 s 是否为t 的自序列, 我们自需要顺序遍历 t ,然后对比是否 s中也出现, 并且相对顺序不能变更即可。 代码实现中有。
用户11097514
2024/05/31
1140
力扣每日一刷(2023.9.21)
动态规划经典模型:双数组问题的通用解决框架与实战
最长公共子序列 给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度。如果不存在 公共子序列 ,返回 0 。
Undoom
2025/08/09
950
动态规划经典模型:双数组问题的通用解决框架与实战
dp[i][j]表示以i - 1结尾的s里 有多少个 以j - 1为结尾的t
from torch.utils.tensorboard import SummaryWriter
用户7737280
2024/08/31
4050
LeetCode 题目解答——Hard 部分
[Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复杂度上根本就不是最优解,有的写的太啰嗦,有的则用了一些过于 tricky 的方法。我没有为了这个再更新,就让它们去吧。
四火
2022/07/19
1.5K0
LeetCode 题目解答——Hard 部分
LeetCode 0115. 不同的子序列[动态规划详解]
字符串的一个 子序列 是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串。(例如,"ACE" 是 "ABCDE" 的一个子序列,而 "AEC" 不是)
Yano_nankai
2021/03/21
7940
LeetCode 0115. 不同的子序列[动态规划详解]
字符串基础题
总结:所有题目都已做,有些Easy没有做第二遍,有两道没有accept,请戳 link-en, link-cn
王脸小
2019/11/02
1K0
115. 不同的子序列
一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串。(例如,”ACE” 是 “ABCDE” 的一个子序列,而 “AEC” 不是)
张伦聪zhangluncong
2022/10/26
4890
相关推荐
Leetcode 115 Distinct Subsequences
更多 >
交个朋友
加入腾讯云官网粉丝站
蹲全网底价单品 享第一手活动信息
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档