LeetCode //C - 115. Distinct Subsequences

115. Distinct Subsequences

Given two strings s and t, return the number of distinct subsequences of s which equals t.

The test cases are generated so that the answer fits on a 32-bit signed integer.

Example 1:

Input: s = "rabbbit", t = "rabbit"
Output: 3
Explanation:

As shown below, there are 3 ways you can generate "rabbit" from s.

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.

babgbag

babgbag

babgbag

babgbag

babgbag

Constraints:
  • 1 <= s.length, t.length <= 1000
  • s and t consist of English letters.

From: LeetCode

Link: 115. Distinct Subsequences


Solution:

Ideas:

1. Initialization:

  • The lengths of the strings s and t are determined.
  • A 2D array dp of size (s_len + 1) x (t_len + 1) is initialized. This array will store the number of distinct subsequences of substrings of s that match substrings of t.

2. Base Case:

  • If t is an empty string (length 0), there is exactly one subsequence of any prefix of s that matches it, which is the empty subsequence. Therefore, for all i from 0 to s_len, dpi0 is set to 1.

3. Dynamic Programming Table Filling:

  • The table dp is filled row by row. For each character in s (indexed by i) and each character in t (indexed by j), the following logic is applied:
    • If si-1 is equal to tj-1, it means we can consider two cases:
      1. Using the character si-1 in the subsequence, which would contribute dpi-1j-1 subsequences.
      2. Not using the character si-1, which would contribute dpi-1j subsequences.
    • If si-1 is not equal to tj-1, we cannot use the character si-1 in the subsequence, so the count is simply dpi-1j.
  • These results are combined to update dpij.

4. Result:

  • The value in dps_lent_len gives the number of distinct subsequences of the entire string s that match the entire string t.
Code:
c 复制代码
int numDistinct(char* s, char* t) {
    int s_len = strlen(s);
    int t_len = strlen(t);
    
    // dp array
    unsigned long long dp[s_len + 1][t_len + 1];
    
    // Initialize all elements to 0
    for (int i = 0; i <= s_len; i++) {
        for (int j = 0; j <= t_len; j++) {
            dp[i][j] = 0;
        }
    }
    
    // If t is an empty string, there is exactly one subsequence of any prefix of s that matches it
    for (int i = 0; i <= s_len; i++) {
        dp[i][0] = 1;
    }
    
    // Fill the dp array
    for (int i = 1; i <= s_len; i++) {
        for (int j = 1; j <= t_len; j++) {
            // If characters match, sum the counts of both options (using or not using the current character of s)
            if (s[i - 1] == t[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
            } else {
                // If characters do not match, we can only ignore the current character of s
                dp[i][j] = dp[i - 1][j];
            }
        }
    }
    
    return (int)dp[s_len][t_len];
}
相关推荐
小羊在睡觉3 小时前
力扣84. 柱状图中最大的矩形
后端·算法·leetcode·golang·go
3DVisionary3 小时前
蓝光三维扫描:医疗制造的精度焦虑怎么解
人工智能·算法·制造·蓝光三维扫描·医疗制造·三维检测·义齿检测
好评笔记3 小时前
机器学习面试八股——常用损失函数
人工智能·深度学习·算法·机器学习·校招
weixin_468466853 小时前
全局与局部注意力机制新手实战指南
人工智能·python·深度学习·算法·自然语言处理·transformer·注意力机制
sheeta19984 小时前
LeetCode 每日一题笔记 日期:2026.05.29 题目:3300. 最小元素
笔记·leetcode
_日拱一卒4 小时前
LeetCode:994腐烂的橘子
java·数据结构·算法·leetcode·深度优先
珂朵莉MM4 小时前
第七届全球校园人工智能算法精英大赛-算法巅峰赛产业命题赛第3赛季优化题--束搜索
人工智能·算法
Omics Pro5 小时前
首个!外源天然产物综合性代谢图谱
数据库·人工智能·算法·机器学习·r语言
voidmort5 小时前
3. 微调(Fine-tuning)与强化学习(RL)的核心思想
python·深度学习·算法
Bluetooth7305 小时前
c语言一维数组
c语言