Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode 0115 - Distinct Subsequences

LeetCode 0115 - Distinct Subsequences

作者头像
Reck Zhang
发布于 2021-08-11 02:54:37
发布于 2021-08-11 02:54:37
41700
代码可运行
举报
文章被收录于专栏: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 0392 - Is Subsequence
Given a string s and a string t, check if s is subsequence of t.
Reck Zhang
2021/08/11
2180
LeetCode笔记:392. Is Subsequence
这道题最直接的思路就是遍历t,一个个按顺序检查s中的字符是否顺序出现了,如果一直到s的最后一个字符都出现了,而且是符合顺序的,那就返回true,否则返回false。
Cloudox
2021/11/23
1750
Subsequence - 392. Is Subsequence
Given a string s and a string t, check if s is subsequence of t.
ppxai
2020/09/23
2910
leetcode392. Is Subsequence
如何判断字符串s是否是字符串t的一个子序列。子序列是指s中的字母均按照相对位置存在于t中,比如"abc"是"ahbfdc"的一个子序列,但是"axc"就不是"ahbgdc"的一个子序列。
眯眯眼的猫头鹰
2019/03/13
3080
LeetCode 115. Distinct Subsequences题目分析代码
给出字符串S和字符串T,计算S的不同的子序列中T出现的个数。 子序列字符串是原始字符串通过删除一些(或零个)产生的一个新的字符串,并且对剩下的字符的相对位置没有影响。(比如,“ACE”是“ABCDE”的子序列字符串,而“AEC”不是)。 样例 给出S = "rabbbit", T = "rabbit" 返回 3
desperate633
2018/08/22
5070
​LeetCode刷题实战115:不同的子序列
https://leetcode-cn.com/problems/distinct-subsequences/
程序员小猿
2021/01/19
4280
​LeetCode刷题实战115:不同的子序列
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
7110
1143. Longest Common Subsequence
Given two strings text1 and text2, return *the length of their longest common subsequence. *If there is no common subsequence, return 0.
ppxai
2023/11/18
1660
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
3860
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
6420
每日算法系列【LeetCode 115】不同的子序列
一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串。(例如,"ACE" 是 "ABCDE" 的一个子序列,而 "AEC" 不是)
godweiyang
2020/03/24
9360
LeetCode 115. 不同的子序列(DP)
一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串。(例如,“ACE” 是 “ABCDE” 的一个子序列,而 “AEC” 不是)
Michael阿明
2021/02/19
3420
[Leetcode][python]Distinct Subsequences/不同子序列
同样你可以打印出dp看结构:上半区都为0,因为不可能,dp[0][0]为1因为空转空有一种可能(不删除)
蛮三刀酱
2019/03/26
6000
力扣每日一刷(2023.9.21)
本题其实不使用动态规划的思路也是能够解出来的 ,并且时间复杂度 和 空间复杂度更低。 因为题目中问的是 s 是否为t 的自序列, 我们自需要顺序遍历 t ,然后对比是否 s中也出现, 并且相对顺序不能变更即可。 代码实现中有。
用户11097514
2024/05/31
950
力扣每日一刷(2023.9.21)
LeetCode 392. 判断子序列(双指针&二分查找)
你可以认为 s 和 t 中仅包含英文小写字母。字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=100)。
Michael阿明
2020/07/13
9520
LeetCode 392. 判断子序列(双指针&二分查找)
动态规划:判断子序列
题目链接:https://leetcode-cn.com/problems/is-subsequence/
代码随想录
2021/04/07
6650
动态规划:判断子序列
【动态规划算法练习】day13
115. 不同的子序列 给你两个字符串 s 和 t ,统计并返回在 s 的 子序列 中 t 出现的个数。 题目数据保证答案符合 32 位带符号整数范围。
摘星
2023/10/15
1680
【动态规划算法练习】day13
792. Number of Matching Subsequences
思路: 从note中可以看出words的长度不长,而S的长度最大有50000,暴力的做法:每个word去匹配S,因为S没有记忆成高效数据结构,每次匹配都会重新遍历一次S,时间复杂度为O(len(S)),显然会超时。
用户1147447
2019/05/26
4500
【算法】动态规划:回文子串问题、两个数组的dp
定义 dp[i][j] 表示 [i, j] 区间内的字符串是否是回文子串,i <= j,要特别注意填表顺序。
_小羊_
2025/03/27
1170
【算法】动态规划:回文子串问题、两个数组的dp
一招解决4道leetcode hard题,动态规划在字符串匹配问题中的应用
在做leetcode的时候,遇到hard题大家往往都觉得头疼,但其实,掌握方法,举一反三,hard题有时候我们也能想到好的思路,顺利攻破,今天我们就介绍一下动态规划在字符串匹配中的应用,相同类型的题目在前120道题中居然出现了4次!有必要好好总结一下! 这四道题分别是: 10. Regular Expression Matching:https://leetcode.com/problems/regular-expression-matching/description. 44.Wildcard Match
石晓文
2018/04/11
4.7K0
一招解决4道leetcode hard题,动态规划在字符串匹配问题中的应用
相关推荐
LeetCode 0392 - Is Subsequence
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验