[LeetCode] Distinct Subsequences

简介: Well, a dynamic programming problem. Let's first define its state dp[i][j] to be the number of distinct subsequences of t[0.

Well, a dynamic programming problem. Let's first define its state dp[i][j] to be the number of distinct subsequences of t[0..i - 1] in s[0..j - 1]. Then we have the following state equations:

  1. General case 1: dp[i][j] = dp[i][j - 1] if t[i - 1] != s[j - 1];
  2. General case 2: dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1] if t[i - 1] == s[j - 1];
  3. Boundary case 1: dp[0][j] = 1 for all j;
  4. Boundary case 2: dp[i][0] = 0 for all positive i.

Now let's give brief explanations to the four equations above.

  1. If t[i - 1] != s[j - 1], the distinct subsequences will not include s[j - 1] and thus all the number of distinct subsequences will simply be those in s[0..j - 2], which corresponds to dp[i][j - 1];
  2. If t[i - 1] == s[j - 1], the number of distinct subsequences include two parts: those withs[j - 1] and those without;
  3. An empty string will have exactly one subsequence in any string :-)
  4. Non-empty string will have no subsequences in an empty string.

Putting these together, we will have the following simple codes (just like translation :-)):

 1 class Solution {
 2 public:
 3     int numDistinct(string s, string t) {
 4         int m = t.length(), n = s.length();
 5         vector<vector<int>> dp(m + 1, vector<int> (n + 1, 0));
 6         for (int j = 0; j <= n; j++) dp[0][j] = 1;
 7         for (int j = 1; j <= n; j++)
 8             for (int i = 1; i <= m; i++)
 9                 dp[i][j] = dp[i][j - 1] + (t[i - 1] == s[j - 1] ? dp[i - 1][j - 1] : 0);
10         return dp[m][n];
11     }
12 };

Notice that we keep the whole m*n matrix simply for dp[i - 1][j - 1]. So we can simply store that value in a single variable and further optimize the space complexity. The final code is as follows.

 1 class Solution {
 2 public:
 3     int numDistinct(string s, string t) {
 4         int m = t.length(), n = s.length();
 5         vector<int> cur(m + 1, 0);
 6         cur[0] = 1;
 7         for (int j = 1; j <= n; j++) { 
 8             int pre = 1;
 9             for (int i = 1; i <= m; i++) {
10                 int temp = cur[i];
11                 cur[i] = cur[i] + (t[i - 1] == s[j - 1] ? pre : 0);
12                 pre = temp;
13             }
14         }
15         return cur[m];
16     }
17 };

 

目录
相关文章
LeetCode 115. Distinct Subsequences
给定字符串S和字符串T,计算S的不同子序列的数量,使其其等于T. 字符串的子序列是一个新字符串,它是通过删除一些(可以是无)字符而不干扰其余字符的相对位置而从原始字符串形成的。 (即,“ACE”是“ABCDE”的子序列,而“AEC”不是)。
177 0
LeetCode 115. Distinct Subsequences
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
275 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
176 6
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
384 2
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
288 3
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
机器学习/深度学习 人工智能 自然语言处理
280页PDF,全方位评估OpenAI o1,Leetcode刷题准确率竟这么高
【10月更文挑战第24天】近年来,OpenAI的o1模型在大型语言模型(LLMs)中脱颖而出,展现出卓越的推理能力和知识整合能力。基于Transformer架构,o1模型采用了链式思维和强化学习等先进技术,显著提升了其在编程竞赛、医学影像报告生成、数学问题解决、自然语言推理和芯片设计等领域的表现。本文将全面评估o1模型的性能及其对AI研究和应用的潜在影响。
360 1

热门文章

最新文章