115. Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S.
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).
Here is an example:
S = “rabbbit”, T = “rabbit”
Return 3.
题意:
给定一个字符串S和字符串T,统计不同的子序列T的在S中出现的数量。
字符串的子序列是一个新字符串,它从原始字符串中形成,通过删除一些(可以是没有)字符而不干扰其余字符的相对位置。(例如“ACE”是“ABCDE”的一个子序列,而“AEC”不是)。
思路:
题目给定两个字符串,选择只可以用删除字符的方法从第一个字符串变换到第二个字符串,求出一共有多少种变换方法;动态规划思想应用的典型题目。
1 | 定义二维数组dp[i][j]为s[0, i)中包含多少个值为t[0, j)的子序列。 |
1 | class Solution { |