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];
}
相关推荐
_OP_CHEN2 分钟前
【算法基础篇】(四十三)数论之费马小定理深度解析:从同余性质到乘法逆元
c++·算法·蓝桥杯·数论·acm/icpc
水月wwww5 分钟前
【算法设计】分支限界法
算法·分支限界法
茶猫_11 分钟前
C++学习记录-旧题新做-链表求和
数据结构·c++·学习·算法·leetcode·链表
yuniko-n18 分钟前
【牛客面试 TOP 101】链表篇(一)
数据结构·算法·链表·面试·职场和发展
2501_9418053126 分钟前
从微服务网关到统一安全治理的互联网工程语法实践与多语言探索
前端·python·算法
源代码•宸27 分钟前
Leetcode—1161. 最大层内元素和【中等】
经验分享·算法·leetcode·golang
CodeByV1 小时前
【算法题】模拟
算法
JAY_LIN——81 小时前
C语言内存函数memcpy、memmove、menset、mencmp
c语言·开发语言
s09071361 小时前
FPGA加速:Harris角点检测全解析
图像处理·算法·fpga开发·角点检测
前端程序猿之路1 小时前
30天大模型学习之Day 2:Prompt 工程基础系统
大数据·人工智能·学习·算法·语言模型·prompt·ai编程