2021-08-14:给定两个字符串S和T,返回S的所有子序列中有多少个子序列的字面值等于T。
福大大 答案2021-08-14:
样本对应模型。
时间复杂度:O(N^2)。
空间复杂度:O(N^2)。
代码用golang编写。代码如下:
package main
import "fmt"
func main() {
s := "1122"
t := "12"
ret := numDistinct1(s, t)
fmt.Println(ret)
}
func numDistinct1(S string, T string) int {
return process(S, T, len(S), len(T))
}
func process(s string, t string, i int, j int) int {
if j == 0 {
return 1
}
if i == 0 {
return 0
}
res := process(s, t, i-1, j)
if s[i-1] == t[j-1] {
res += process(s, t, i-1, j-1)
}
return res
}
执行结果如下:
***
[左神java代码](https://github.com/algorithmzuo/coding-for-great-offer/blob/main/src/class17/Code04_DistinctSubseq.java)
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有