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


题目地址:https://leetcode.com/problems/count-different-palindromic-subsequences/description/

题目描述

Given a string S, find the number of different non-empty palindromic subsequences in S, and return that number modulo 10^9 + 7.

A subsequence of a string S is obtained by deleting 0 or more characters from S.

A sequence is palindromic if it is equal to the sequence reversed.

Two sequences A_1, A_2, ... and B_1, B_2, ... are different if there is some i for which A_i != B_i.

Example 1:

Input:
S = 'bccb'
Output: 6
Explanation:
The 6 different non-empty palindromic subsequences are 'b', 'c', 'bb', 'cc', 'bcb', 'bccb'.
Note that 'bcb' is counted only once, even though it occurs twice.

Example 2:

Input:
S = 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba'
Output: 104860361
Explanation:
There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 10^9 + 7.

Note:

  • The length of S will be in the range [1, 1000].
  • Each character S[i] will be in the set {‘a’, ‘b’, ‘c’, ‘d’}.

题目大意

求一个字符串的有多少个回文子序列。

解题方法

记忆化搜索

这个题太难了,我也只是抄了花花酱的答案,花花有个40分钟的视频,讲得非常清楚,强烈大家看看。

class Solution:
def countPalindromicSubsequences(self, S):
"""
:type S: str
:rtype: int
"""
def count(S, i, j):
if i > j: return 0
if i == j: return 1
if self.m_[i][j]:
return self.m_[i][j]
if S[i] == S[j]:
ans = count(S, i + 1, j - 1) * 2
l = i + 1
r = j - 1
while l <= r and S[l] != S[i]: l += 1
while l <= r and S[r] != S[i]: r -= 1
if l > r: ans += 2
elif l == r: ans += 1
else: ans -= count(S, l + 1, r - 1)
else:
ans = count(S, i + 1, j) + count(S, i, j - 1) - count(S, i + 1, j - 1) self.m_[i][j] = ans % (10 ** 9 + 7)
return self.m_[i][j] n = len(S)
self.m_ = [[None for _ in range(n)] for _ in range(n)]
return count(S, 0, n - 1)

动态规划

待补

日期

2018 年 11 月 17 日 —— 美妙的周末,美丽的天气

【LeetCode】730. Count Different Palindromic Subsequences 解题报告(Python)的更多相关文章

  1. leetcode 730 Count Different Palindromic Subsequences

    题目链接: https://leetcode.com/problems/count-different-palindromic-subsequences/description/ 730.Count ...

  2. LN : leetcode 730 Count Different Palindromic Subsequences

    lc 730 Count Different Palindromic Subsequences 730 Count Different Palindromic Subsequences Given a ...

  3. [LeetCode] 730. Count Different Palindromic Subsequences 计数不同的回文子序列的个数

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  4. 730. Count Different Palindromic Subsequences

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  5. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

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

  6. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

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

  7. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

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

  8. 【LeetCode】647. Palindromic Substrings 解题报告(Python)

    [LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...

  9. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

随机推荐

  1. miRNA 基本知识

    miRNA MicroRNA (miRNA)  是一类内生的.长度约为20-24个核苷酸的小 RNA,其在细胞内具有多种重要的调节作用.每个 miRNA 可以有多个靶基因的表达,而几个 miRNA 也 ...

  2. 卷积神经网络(Convolutional Neural Networks)CNN

     申明:本文非笔者原创,原文转载自:http://www.36dsj.com/archives/24006 自今年七月份以来,一直在实验室负责卷积神经网络(Convolutional Neural ...

  3. Centos7部署RabbitMQ的镜像队列集群

    一.背景 在上一章节中,我们学会了如何搭建一个单节点的RabbitMQ服务器,但是单节点的RabbitMQ不可靠,如果单节点挂掉,则会导致消息队列不可用.此处我们搭建一个3个节点的RabbitMQ集群 ...

  4. Spark(二十)【SparkSQL将CSV导入Kudu】

    目录 SparkSql 将CSV导入kudu pom 依赖 scala 代码 启动脚本 SparkSql 将CSV导入kudu pom 依赖 <properties> <spark. ...

  5. Web安全学习二

    目录 常见漏洞攻防 SQL注入 注入分类 按技巧分类 按获取数据的方式分类 注入检测 权限提升 数据库检测 绕过技巧 CheatSheet SQL Server Payload MySQL Paylo ...

  6. 使用 ACE 库框架在 UNIX 中开发高性能并发应用

    使用 ACE 库框架在 UNIX 中开发高性能并发应用来源:developerWorks 中国 作者:Arpan Sen ACE 开放源码工具包可以帮助开发人员创建健壮的可移植多线程应用程序.本文讨论 ...

  7. react中在hooks方法useEffect中加载异步数据

    useEffect( ()=>{ (async function getPipeList(value:any) { let result= await GetPipeList(value); s ...

  8. 3.使用Spring Data ElasticSearch操作ElasticSearch(5.6.8版本)

    1.引入maven坐标 <!--spring-data-elasticsearch--><dependency> <groupId>org.springframew ...

  9. 8.Vue.js-计算属性

    计算属性关键词: computed. 计算属性在处理一些复杂逻辑时是很有用的. 可以看下以下反转字符串的例子: <!DOCTYPE html><html><head> ...

  10. C# 温故知新 第二篇 C# 程序的通用结构

    C# 程序由一个或多个文件组成. 每个文件均包含零个或多个命名空间. 一个命名空间包含类.结构.接口.枚举.委托等类型或其他命名空间. 以下示例是包含所有这些元素的 C# 程序主干. 主要包括  1. ...