LeetCode:115. Distinct Subsequences
题目
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).
Example 1:
Input: S = “rabbbit”, T = “rabbit”
Output: 3
Explanation:
As shown below, there are 3 ways you can generate “rabbit” from S.
(The caret symbol ^ means the chosen letters)
rabbbit
^^^^ ^^
rabbbit
^^ ^^^^
rabbbit
^^^ ^^^
Example 2:
Input: S = “babgbag”, T = “bag”
Output: 5
Explanation:
As shown below, there are 5 ways you can generate “bag” from S.
(The caret symbol ^ means the chosen letters)
babgbag
^^ ^
babgbag
^^ ^
babgbag
^ ^^
babgbag
^ ^^
babgbag
^^^
解题思路
看了大佬们的解题思路,其实有点复杂,有点慢。
- 新建一个res[t_len+1, s_len+1]的全0矩阵,并令第一行为1
- 当t[i-1]==s[j-1]的时候,res[i][j] = res[i][j-1] + res[i-1][j-1]
- 否则 res[i][j] = res[i][j-1]
我第一条写得特别不美观!嫌弃!!!
class Solution:def numDistinct(self, s: str, t: str) -> int:l1, l2 = len(s)+1, len(t)+1res = []for i in range(len(t)+1):tmp = []for j in range(len(s)+1):tmp.append(0)res.append(tmp)for j in range(len(s)+1):res[0][j]=1for i in range(1,len(t)+1):for j in range(1, len(s)+1):print(t[i-1], s[j-1])if t[i-1]==s[j-1]:res[i][j] = res[i][j-1] + res[i-1][j-1] else:res[i][j] = res[i][j-1]return res[l2-1][l1-1]
LeetCode:115. Distinct Subsequences相关推荐
- 【重点!DP】LeetCode 115. Distinct Subsequences
LeetCode 115. Distinct Subsequences Solution1: 不会做.. 参考网址:https://www.youtube.com/watch?v=mPqqXh8XvW ...
- leetcode 115. Distinct Subsequences Hard | 115. 不同的子序列(动态规划)
题目 https://leetcode.com/problems/distinct-subsequences/ 题解 方法1:递归(超时) 这种解法比较容易理解,时间复杂度没算出来,但肯定不是 O(m ...
- 【Leetcode】115. Distinct Subsequences
又是一道DP的题目,个人感觉DP的题目比较难,主要因为:(1)DP的难点是寻找子问题,如果找到很好的子问题,那么就可以瞬间搞定.(2)通常也会带有一点backtracking的思想,有时候总是优先想到 ...
- lc 115. Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/description/ 求串s中有几个字串t? 开始看成了s和t有几个公共字串,想来也是dp. ...
- Distinct Subsequences@LeetCode
Distinct Subsequences 动态规划题.先用二维动态规划的思路解释下:设match是动态规划表,其中match[i][j]表示S.substring(0, i)对T.substring ...
- [LeetCode]Distinct Subsequences,解题报告
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- 【leetcode】940. Distinct Subsequences II
题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...
- leetcode Distinct Subsequences
字符串处理与二维dp 设置二维数组v[i][j]表示S串中前i个字母的子串中包含多少个T串中前j个字母的子串"这样的问题记为A[i][j]. 可以得到递推式 : if(S[i-1] == T ...
- Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/discuss/37327/Easy-to-understand-DP-in-Java 如果S[ ...
最新文章
- STC单片机下载实验
- php 5.2.17 mysql_Apache 2.2.15 整合php 5.2.17 Mysql-5.5.8
- NYOJ 975 关于521
- 学习编程的基础四大件
- httpclient base64 文件上传_文件上传下载
- 39个转录组分析工具,120种组合评估
- 近期海内外 AI 领域招聘、招生信息汇总
- Ubuntu 想在 Windows 的 WSL 中做到领先
- 三星 SGH-G810 多普达 P800 多普达 Touch Diamond(S900) 多普达 P860 多普达 Touch(T3238) 对比...
- Apache OpenNLP(二)
- 同步图计算:GraphLite的安装和使用
- matlab dpabi安装,Android 8 应用安装时 ABI 确认过程
- linux救援模式详解,Linux系统的救援模式应用详解
- SMP多核启动(二):PSCI
- EtherCAT总线运动控制器中简单易用的直线插补
- iOS远程真机之ios-minicap安装使用完全指南
- php cookie start,有关php session和Cookie问题session_start开启后做什么事?
- 技巧:如何提高git下载速度
- NR 5G SRB的定义和类型
- [SDOI2008]Sue的小球(区间Dp)
热门文章
- UITesting Bundle使用
- OpenStack平台功能性测试工具Tempest安装
- areas where akka is being deployed into production
- Select2 的简单使用
- hdu 2041:超级楼梯(水题,递归)
- CSS样式学习-CSS 背景
- Missing separate debuginfos, use: debuginfo-install
- 软件测试理论你知道多少?
- Anaconda系列:conda是什么?conda与pip的区别是什么?
- Python 列表(List) 取区间元素 [:] 用法