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"

Return3.

C++


  1. class Solution {
  2. public:
  3. int numDistinct(string S, string T) {
  4. S=" "+S,T=" "+T;
  5. int n=S.length();
  6. int m=T.length();
  7. int i;
  8. int j;
  9. vector<vector<int> > dp(n+1,vector<int>(m+1,0));
  10. for(i=0;i<=n;i++) dp[i][0]=1;
  11. for(i=1;i<=n;i++)
  12. for(j=1;j<=m;j++){
  13. dp[i][j]=dp[i-1][j];
  14. if(S[i]==T[j]) dp[i][j]+=dp[i-1][j-1];
  15. }
  16. return dp[n][m];
  17. }
  18. };

distinct-subsequences leetcode C++的更多相关文章

  1. Distinct Subsequences Leetcode

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  2. Distinct Subsequences——Leetcode

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  3. Distinct Subsequences leetcode java

    题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...

  4. Java for LeetCode 115 Distinct Subsequences【HARD】

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  5. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  6. 【LeetCode OJ】Distinct Subsequences

    Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...

  7. leetcode@ [72/115] Edit Distance & Distinct Subsequences (Dynamic Programming)

    https://leetcode.com/problems/edit-distance/ Given two words word1 and word2, find the minimum numbe ...

  8. 【一天一道LeetCode】#115. Distinct Subsequences

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. LeetCode之“动态规划”:Distinct Subsequences

    题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...

  10. [leetcode]Distinct Subsequences @ Python

    原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...

随机推荐

  1. php在类中使用回调函数 如array_map

    <?php class foo {   var $var;   function bar() {      array_map(array($this, "baz"), ar ...

  2. bzoj#4423-[AMPPZ2013]Bytehattan【并查集】

    正题 题目链接:https://darkbzoj.tk/problem/4423 题目大意 给出一个\(n*n\)的网格图,然后四联通的点之间连接.每次删掉一条边求这条边的两个点是否连通.强制在线. ...

  3. Loj#2769-「ROI 2017 Day 1」前往大都会【最短路树,斜率优化】

    正题 题目链接:https://loj.ac/p/2769 题目大意 给出\(n\)个点\(m\)条地铁线路,每条线路是一条路径. 求\(1\)到\(n\)的最短路且在最短路径的情况下相邻换乘点的距离 ...

  4. python OSError: [Errno 22] Invalid argument: '\u202aF://text

    windows10 python3 读文件的时候报的错误 原因不明时好时坏很头疼但又没办法不知道怎么解决,网上的说法都不能解决,

  5. 踩坑系列《三》 java -version 报错出现Error occurred during initialization of VM java/lang/NoClassDefFoundError:

    之前导入一个项目,因为该项目Spring版本过低,只能适用于1.7版本,装了jdk1.7版本.安装配置好后,打开cmd窗口,执行 java -version 指令发现没像正常一样显示版本信息,而是报了 ...

  6. Oracle配置tcps加密协议

    1.Oracle用户下操作,创建证书 mkdir /home/oracle/wallet orapki wallet create -wallet "/home/oracle/wallet& ...

  7. LightningChart XY功能中的常见问题

    LightningChart XY功能中的常见问题 XY 是LightningChart 的重要功能之一,也是被用户使用最广泛的.用户经常对这个功能有着这样那样的疑问,现在将一些常用问题汇总了一下,希 ...

  8. 免费 CDN 玩法 —— 文件一键上传到 NPM

    前言 unpkg.jsdelivr 等站点可加速 NPM 包文件,适合作为个人网站或演示案例的免费 CDN. 虽然上传文件到 NPM 很简单,创建 package.json 然后 npm publis ...

  9. 单机CentOS 安装 TiDB

    目录 一.官网教程 二.安装步骤 1.下载并安装 TiUP: 2.声明一下环境变量,否则会找不到 tiup 命令 3.安装 TiUP 的 cluster 组件: 4.官方教程说,由于模拟多机部署,需要 ...

  10. JuiceFS 如何帮助趣头条超大规模 HDFS 降负载

    作者简介 王振华,趣头条大数据总监,趣头条大数据负责人. 王海胜,趣头条大数据工程师,10 年互联网工作经验,曾在 eBay.唯品会等公司从事大数据开发相关工作,有丰富的大数据落地经验. 高昌健,Ju ...