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, dp[i][0] 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 s[i-1] is equal to t[j-1], it means we can consider two cases:
      1. Using the character s[i-1] in the subsequence, which would contribute dp[i-1][j-1] subsequences.
      2. Not using the character s[i-1], which would contribute dp[i-1][j] subsequences.
    • If s[i-1] is not equal to t[j-1], we cannot use the character s[i-1] in the subsequence, so the count is simply dp[i-1][j].
  • These results are combined to update dp[i][j].

4. Result:

  • The value in dp[s_len][t_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];
}
相关推荐
杨福瑞36 分钟前
C语言⽂件操作讲解(总)
c语言·开发语言
Rubisco..1 小时前
牛客周赛 Round 111
数据结构·c++·算法
兮山与2 小时前
算法8.0
算法
高山上有一只小老虎2 小时前
杨辉三角的变形
java·算法
Swift社区2 小时前
LeetCode 395 - 至少有 K 个重复字符的最长子串
算法·leetcode·职场和发展
Espresso Macchiato2 小时前
Leetcode 3710. Maximum Partition Factor
leetcode·职场和发展·广度优先遍历·二分法·leetcode hard·leetcode 3710·leetcode双周赛167
hz_zhangrl2 小时前
CCF-GESP 等级考试 2025年9月认证C++四级真题解析
开发语言·c++·算法·程序设计·gesp·c++四级·gesp2025年9月
少许极端2 小时前
算法奇妙屋(六)-哈希表
java·数据结构·算法·哈希算法·散列表·排序
羊羊小栈2 小时前
基于「多模态大模型 + BGE向量检索增强RAG」的新能源汽车故障诊断智能问答系统(vue+flask+AI算法)
vue.js·人工智能·算法·flask·汽车·毕业设计·大作业
Da Da 泓2 小时前
shellSort
java·数据结构·学习·算法·排序算法