comments | edit_url |
---|---|
true |
给定一个字符串 s
和一个字符串 t
,计算在 s
的子序列中 t
出现的个数。
字符串的一个 子序列 是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串。(例如,"ACE"
是 "ABCDE"
的一个子序列,而 "AEC"
不是)
题目数据保证答案符合 32 位带符号整数范围。
示例 1:
输入:s = "rabbbit", t = "rabbit"输出
:3
解释: 如下图所示, 有 3 种可以从 s 中得到"rabbit" 的方案
。rabbbit
rabbbit
rabbbit
示例 2:
输入:s = "babgbag", t = "bag"输出
:5
解释: 如下图所示, 有 5 种可以从 s 中得到"bag" 的方案
。babgbag
babgbag
babgbag
babgbag
babgbag
提示:
0 <= s.length, t.length <= 1000
s
和t
由英文字母组成
注意:本题与主站 115 题相同: https://leetcode.cn/problems/distinct-subsequences/
我们定义
当
- 当
$s[i-1] \ne t[j-1]$ 时,不能选取$s[i-1]$ ,因此$f[i][j]=f[i-1][j]$ ; - 否则,可以选取
$s[i-1]$ ,此时$f[i][j]=f[i-1][j-1]$ 。
因此我们有如下的状态转移方程:
最终的答案即为
时间复杂度
我们注意到
class Solution:
def numDistinct(self, s: str, t: str) -> int:
m, n = len(s), len(t)
f = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(m + 1):
f[i][0] = 1
for i, a in enumerate(s, 1):
for j, b in enumerate(t, 1):
f[i][j] = f[i - 1][j]
if a == b:
f[i][j] += f[i - 1][j - 1]
return f[m][n]
class Solution {
public int numDistinct(String s, String t) {
int m = s.length(), n = t.length();
int[][] f = new int[m + 1][n + 1];
for (int i = 0; i < m + 1; ++i) {
f[i][0] = 1;
}
for (int i = 1; i < m + 1; ++i) {
for (int j = 1; j < n + 1; ++j) {
f[i][j] = f[i - 1][j];
if (s.charAt(i - 1) == t.charAt(j - 1)) {
f[i][j] += f[i - 1][j - 1];
}
}
}
return f[m][n];
}
}
class Solution {
public:
int numDistinct(string s, string t) {
int m = s.size(), n = t.size();
unsigned long long f[m + 1][n + 1];
memset(f, 0, sizeof(f));
for (int i = 0; i < m + 1; ++i) {
f[i][0] = 1;
}
for (int i = 1; i < m + 1; ++i) {
for (int j = 1; j < n + 1; ++j) {
f[i][j] = f[i - 1][j];
if (s[i - 1] == t[j - 1]) {
f[i][j] += f[i - 1][j - 1];
}
}
}
return f[m][n];
}
};
func numDistinct(s string, t string) int {
m, n := len(s), len(t)
f := make([][]int, m+1)
for i := range f {
f[i] = make([]int, n+1)
}
for i := 0; i <= m; i++ {
f[i][0] = 1
}
for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {
f[i][j] = f[i-1][j]
if s[i-1] == t[j-1] {
f[i][j] += f[i-1][j-1]
}
}
}
return f[m][n]
}
function numDistinct(s: string, t: string): number {
const m = s.length;
const n = t.length;
const f: number[][] = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0));
for (let i = 0; i <= m; ++i) {
f[i][0] = 1;
}
for (let i = 1; i <= m; ++i) {
for (let j = 1; j <= n; ++j) {
f[i][j] = f[i - 1][j];
if (s[i - 1] === t[j - 1]) {
f[i][j] += f[i - 1][j - 1];
}
}
}
return f[m][n];
}
class Solution {
func numDistinct(_ s: String, _ t: String) -> Int {
let m = s.count, n = t.count
let sArray = Array(s)
let tArray = Array(t)
var dp = Array(repeating: Array(repeating: 0, count: n + 1), count: m + 1)
for i in 0...m {
dp[i][0] = 1
}
for i in 1...m {
for j in 1...n {
dp[i][j] = dp[i - 1][j]
if sArray[i - 1] == tArray[j - 1] {
dp[i][j] += dp[i - 1][j - 1]
}
}
}
return dp[m][n]
}
}
class Solution:
def numDistinct(self, s: str, t: str) -> int:
n = len(t)
f = [1] + [0] * n
for a in s:
for j in range(n, 0, -1):
if a == t[j - 1]:
f[j] += f[j - 1]
return f[n]
class Solution {
public int numDistinct(String s, String t) {
int n = t.length();
int[] f = new int[n + 1];
f[0] = 1;
for (char a : s.toCharArray()) {
for (int j = n; j > 0; --j) {
char b = t.charAt(j - 1);
if (a == b) {
f[j] += f[j - 1];
}
}
}
return f[n];
}
}
class Solution {
public:
int numDistinct(string s, string t) {
int n = t.size();
unsigned long long f[n + 1];
memset(f, 0, sizeof(f));
f[0] = 1;
for (char& a : s) {
for (int j = n; j; --j) {
char b = t[j - 1];
if (a == b) {
f[j] += f[j - 1];
}
}
}
return f[n];
}
};
func numDistinct(s string, t string) int {
n := len(t)
f := make([]int, n+1)
f[0] = 1
for _, a := range s {
for j := n; j > 0; j-- {
if b := t[j-1]; byte(a) == b {
f[j] += f[j-1]
}
}
}
return f[n]
}
function numDistinct(s: string, t: string): number {
const n = t.length;
const f: number[] = new Array(n + 1).fill(0);
f[0] = 1;
for (const a of s) {
for (let j = n; j; --j) {
const b = t[j - 1];
if (a === b) {
f[j] += f[j - 1];
}
}
}
return f[n];
}