Given a string S, count the number of distinct, non-empty subsequences of S .

Since the result may be large, return the answer modulo 10^9 + 7.

Example 1:

Input: "abc"
Output: 7
Explanation: The 7 distinct subsequences are "a", "b", "c", "ab", "ac", "bc", and "abc".

Example 2:

Input: "aba"
Output: 6
Explanation: The 6 distinct subsequences are "a", "b", "ab", "ba", "aa" and "aba".

Example 3:

Input: "aaa"
Output: 3
Explanation: The 3 distinct subsequences are "a", "aa" and "aaa".

Note:

  1. S contains only lowercase letters.
  2. 1 <= S.length <= 2000

Approach #1: Math. [Java]

class Solution {
public int distinctSubseqII(String S) {
long end[] = new long[26], mod = (long)1e9 + 7;
for (char c : S.toCharArray())
end[c - 'a'] = Arrays.stream(end).sum()%mod + 1;
return (int)(Arrays.stream(end).sum()%mod);
}
}

  

Analysis:

Reference:

https://www.jianshu.com/p/02501f516437

940. Distinct Subsequences II的更多相关文章

  1. 【leetcode】940. Distinct Subsequences II

    题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...

  2. 【LeetCode】940. Distinct Subsequences II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  3. [Swift]LeetCode940. 不同的子序列 II | Distinct Subsequences II

    Given a string S, count the number of distinct, non-empty subsequences of S . Since the result may b ...

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

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

  5. Distinct Subsequences

    https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...

  6. Leetcode Distinct Subsequences

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

  7. LeetCode(115) Distinct Subsequences

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

  8. [Leetcode][JAVA] Distinct Subsequences

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

  9. Distinct Subsequences Leetcode

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

随机推荐

  1. 《Google软件测试之道》摘录

    以下是最近看的一本书<Google软件测试之道>里的一些摘录,收获很多. 1.讨论测试开发比并没有什么意义,如果你是一名开发人员,同时也是一名测试人员,如果你的职位头衔上有测试的字样,你的 ...

  2. MVC中构建Linq条件、排序、Selector字段过滤

    代码: System.Linq.Expressions.Expression<Func<Domain.S_ROLE, bool>> expressWhere1 = (c =&g ...

  3. [freeCodeCamp] Start a Nodejs Server - Complete "Make it Modular"

    _/home/ubuntu/.nvm/versions/node/v4.4.5/lib/node_modules/learnyounode/exer cises/make_it_modular/sol ...

  4. 通过ANT实现jmeter批量执行脚本、生成报告、发送邮件全套build.xml文件

    在开始通过ANT运行build.xml之前,有一步必须要做,那就是将JMeter所在目录下extras子目录里的ant-JMeter-1.1.1.jar复制到Ant所在目录lib子目录之下,这样Ant ...

  5. [转]微信公众平台(测试接口)开发前的准备工作(转载自walkingmanc的专栏)

    本文转自:http://blog.csdn.net/jiangweicpu/article/details/21228949 http://blog.csdn.net/walkingmanc/arti ...

  6. TLS/SSL简单过程

    .wcf的认证分为两种 1.1 transport模式,在传输层完成认证(只能在传输层完成认证,利用硬件加速效率更高) a.在使用transport模式,非windows凭证的情况下默认使用TLS/S ...

  7. 设计模式之prototype(原型模型)

    以我的理解原型模式的重点就几个字 用于克隆易变对象设计模式主要是为了解决new对象时的耦合问题,这是要克隆某个易变对象时只要直接传入要克隆的对象就可以实现克隆一下是我复制网上的一些代码 摘自<设 ...

  8. mysql 导入导出摘要

    1.导入by数据文件. mysql>load data infile '文件路径' into table 表名 fields terminated by '字段分隔符' lines termin ...

  9. samtools 工具

    软件地址: http://www.htslib.org/ 功能三大版块 : Samtools Reading/writing/editing/indexing/viewing SAM/BAM/CRAM ...

  10. 20155333 2016-2017-2 《Java程序设计》第五周学习总结

    20155333 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 1.使用try.catch语法 与C语言中程序流程和错误处理混在一起不同,Java中把正常流 ...