作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/distinct-subsequences-ii/description/

题目描述

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

题目大意

计算一个字符串中,有多少种不同的子序列。

解题方法

动态规划

周赛的第四题,不会做,还是因为我的动态规划太弱了。。

瞻仰一下寒神的做法吧,膜拜![C++/Java/Python] 4 lines O(N) Time, O(1) Space

使用一个endswith[26]数组,保存的是有多少个子序列以i结尾。则,当前总共有N = sum(endswith)个不同的子序列,当我们新增加一个字符c时,相当于在以前每个结尾的位置后面又增添了一个新的字符,所以现在有了N个以c结尾的不同的子序列了。

所以,我们遍历一遍s,更新的方式是end[c] = sum(end) + 1。加一是因为c本身也是一个子序列。

比如举个例子。

Input: "aba"
Current parsed: "ab" endswith 'a': ["a"]
endswith 'b': ["ab","b"] "a" -> "aa"
"ab" -> "aba"
"b" -> "ba"
"" -> "a" endswith 'a': ["aa","aba","ba","a"]
endswith 'b': ["ab","b"]
result: 6

时间复杂度是O(26N),空间复杂度是O(1)。

class Solution(object):
def distinctSubseqII(self, S):
"""
:type S: str
:rtype: int
"""
nums = [0] * 26
for s in S:
nums[ord(s) - ord("a")] = (sum(nums) + 1) % (10 ** 9 + 7)
return sum(nums) % (10 ** 9 + 7)

日期

2018 年 11 月 11 日 —— 剁手节快乐

【LeetCode】940. Distinct Subsequences II 解题报告(Python)的更多相关文章

  1. 【LeetCode】90. Subsets II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

  2. 【leetcode】940. Distinct Subsequences II

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

  3. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  4. 940. Distinct Subsequences II

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

  5. 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...

  6. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

  7. 【LeetCode】52. N-Queens II 解题报告(Python & C+)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 全排列函数 回溯法 日期 题目地址:https:// ...

  8. 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...

  9. 【LeetCode】275. H-Index II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index- ...

随机推荐

  1. MYSQL5.8-----5

  2. perl 数组快速去除重复元素

    这里记录两种perl数组去重的办法,一种利用哈希(hash),一种直接利用perl自带的模块List::MoreUtils内部的函数uniq. 一.利用hash去重 示例代码如下: 1 #!/usr/ ...

  3. 16-4SUM

    4.30更新,已经AC 18. 4Sum My Submissions QuestionEditorial Solution Total Accepted: 71102 Total Submissio ...

  4. YARP+AgileConfig 5分钟实现一个支持配置热更新的代理网关

    YARP 是微软开源的一个反向代理项目,英文名叫 Yet Another Reverse Proxy .所谓反向代理最有名的那就是 nginx 了,没错 YARP 也可以用来完成 nginx 的大部分 ...

  5. Linux定时任务crontable简介

    Linux下定时执行任务的方法:Linux之crond 服务介绍:https://www.cnblogs.com/liang-io/p/9596294.html http://www.mamicode ...

  6. 技术预演blog

    canal整合springboot实现mysql数据实时同步到redis spring+mysql集成canal springboot整合canal监控mysql数据库 SpringBoot cana ...

  7. 【Java 设计】如何优雅避免空指针调用

    空指针引入 为了避免空指针调用,我们经常会看到这样的语句 if (someobject != null) { someobject.doCalc();} 最终,项目中会存在大量判空代码,多么丑陋繁冗! ...

  8. Go语言核心36讲(Go语言实战与应用二十四)--学习笔记

    46 | 访问网络服务 前导内容:socket 与 IPC 人们常常会使用 Go 语言去编写网络程序(当然了,这方面也是 Go 语言最为擅长的事情).说到网络编程,我们就不得不提及 socket. s ...

  9. Jenkins构建通知

    目录 一.简介 二.推送到gitlab 三.邮件通知 自带配置 Email Extension 四.钉钉通知 五.脚本钉钉通知 六.HTTP请求通知 一.简介 类似于监控报警,jenkins在配置持续 ...

  10. SQL Server 2014如何DATEDIFF()函数截取对应时间年月日

    4.1 定义和用法: DATEDIFF()函数返回两个日期之间的时间 4.2 语法 DATEDIFF(datepart,startdate,enddate) datepart值: year | qua ...