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. sqlserver查询区分大小写

    例子: select * from tb_students where name='jay' select * from tb_students where name='JAY' 这两句查询结果是一样 ...

  2. [SoapUI] 配置默认环境的properties

    <Envs> <Env id="Live,Default environment"> <Project> <CusProperty nam ...

  3. dateframe_loc.iloc.ix

    import pandas as pddf=pd.DataFrame({ "a":[1,2,3], "b":[4,5,6], "c":[7, ...

  4. 继续修改爬虫百度贴吧,这次随意贴吧的任何一个index页都行,然后自动d盘生成tupian文件夹来保存

    from urllib.request import urlopenfrom bs4 import BeautifulSoupfrom urllib.request import urlopenimp ...

  5. 【Maven】安装及配置(Linux)

    本文介绍Linux环境下安装Maven 安装环境和软件 系统:Linux(CentOS) 软件:apache-maven-3.3.9-bin.tar.gz(解压版). 安装步骤 maven是基于Jav ...

  6. 2018.09.07 Amount of degrees(数位dp)

    描述 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的B的整数次幂之和. 例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足题意: 17 = 24+20, ...

  7. phalApi数据库操作

    在很多时候,我们会遇到数据库表里面的某个值需要+1操作,我们不能简单地在update的时候写入array('key' => 'key+1'),因为在解析sql的时候,key+1 会带上引号作为一 ...

  8. SQL之mysql常用操作语句(入门级)

    1.进入数据库: mysql -u root -p mysql -h localhost -u root -p database_name 2.列出数据库 show databases; 3.选择数据 ...

  9. CentOS7+Nginx+多个Tomcat配置

    转载自:https://blog.csdn.net/name_chc/article/details/73332272:亲测可用,加了一些注释: 配置多个tomcat转发 另附上tomcat启动慢的解 ...

  10. python将字典中的数据保存到文件中

    d = {'a':'aaa','b':'bbb'}s = str(d)f = open('dict.txt','w')f.writelines(s)f.close()