[leetcode]Distinct Subsequences
来源:互联网 发布:visual studio c编程 编辑:程序博客网 时间:2024/04/29 23:27
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
.
class Solution {public: int numDistinct(string S, string T) { // Start typing your C/C++ solution below // DO NOT write int main() function int M = S.size(), N = T.size(); if(M == 0 || N == 0) return 0; vector<vector<int>> f(M, vector<int>(N, 0)); f[0][0] = S[0] == T[0] ? 1 : 0; for(int i = 1; i < M; i++){ if(S[i] == T[0]){ f[i][0] = f[i-1][0] + 1; }else{ f[i][0] = f[i-1][0]; } } for(int i = 1; i < M; i++){ for(int j = 1; j < N; j++){ if(S[i] == T[j]){ f[i][j] = f[i-1][j-1] + f[i-1][j]; }else{ f[i][j] = f[i-1][j]; } } } return f[M-1][N-1]; }};
- LeetCode Distinct Subsequences
- [LeetCode]Distinct Subsequences
- [Leetcode] Distinct Subsequences *
- [leetcode] Distinct Subsequences
- [LeetCode] Distinct Subsequences
- [Leetcode] Distinct Subsequences
- leetcode 111: Distinct Subsequences
- LeetCode Distinct Subsequences
- [leetcode] Distinct Subsequences
- LeetCode - Distinct Subsequences
- Leetcode:Distinct Subsequences
- [LeetCode]Distinct Subsequences
- [LeetCode] Distinct Subsequences
- [Leetcode]Distinct Subsequences
- [leetcode]Distinct Subsequences
- Leetcode: Distinct Subsequences
- Leetcode: Distinct Subsequences
- [leetcode] Distinct Subsequences
- 黑马程序员_毕向东_Java基础_DAY03_循环控制_学习笔记
- [unity基础知识]之Unity3d之Vector3 学习与应用
- 顺序容器:vector的自增长特性
- Android 开发中,如何将 SQLite 和 APK 一起打包发布
- GIT 初学笔记--建立一个项目
- [leetcode]Distinct Subsequences
- 职业性格类型
- 将MATLAB数据保存到Excel中
- pdu 7bit 编码
- JDBC连接SQL Server2008
- 关于ORBACUS产品网络通讯函数出现的问题
- A FLASH Bootloader for PIC16 and PIC18 Devices--硬译(三)
- 网格生成
- log4net使用详解(1)