题目如下:

A character is unique in string S if it occurs exactly once in it.

For example, in string S = "LETTER", the only unique characters are "L" and "R".

Let's define UNIQ(S) as the number of unique characters in string S.

For example, UNIQ("LETTER") =  2.

Given a string S with only uppercases, calculate the sum of UNIQ(substring) over all non-empty substrings of S.

If there are two or more equal substrings at different positions in S, we consider them different.

Since the answer can be very large, return the answer modulo 10 ^ 9 + 7.

Example 1:

Input: "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Evey substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10

Example 2:

Input: "ABA"
Output: 8
Explanation: The same as example 1, except uni("ABA") = 1.

Note: 0 <= S.length <= 10000.

解题思路:在任何子串中,只有出现一次的字符才对最终的结果起作用。假设输入的S为 XXXAXXXXAXXAXXXXX,X表示其他任意字符,现在我们来计算蓝A对最后的输出贡献了多少,很显然在两个红A之间的子串中,只要是包括蓝A的子串都有蓝A的贡献,如果第一个红A到蓝A之间的字符数量是L,蓝A到第二个红A之间的字符数量是R,那么蓝A的贡献就是 L + R + L*R + 1 ,其中L表示蓝A与左边字符组成的子串数量,R为与右边的,L*R为同时与左右结合,1表示不与任何字符结合。所有只有找出所有字符左右两边相同字符出现的位置,即可计算出最终的答案。

代码如下:

class Solution(object):
def uniqueLetterString(self, S):
"""
:type S: str
:rtype: int
"""
dic = {}
for i,v in enumerate(S):
dic[v] = dic.setdefault(v,[]) + [i] res = 0
import bisect
for i,v in enumerate(S):
inx = bisect.bisect_left(dic[v], i)
before = i
after = len(S) - i - 1
if inx - 1 >= 0:
before = i - dic[v][inx - 1] - 1
if inx + 1 < len(dic[v]):
after = dic[v][inx + 1] - i - 1
res += (before + after + before * after + 1)
return res % (pow(10, 9) + 7)

【leetcode】828. Unique Letter String的更多相关文章

  1. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  2. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  3. [LeetCode] 828. Unique Letter String 独特字符串

    A character is unique in string S if it occurs exactly once in it. For example, in string S = " ...

  4. 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)

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

  5. 【LeetCode】467. Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  6. 【Leetcode】804. Unique Morse Code Words

    Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...

  7. 【LeetCode】791. Custom Sort String 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 按顺序构造字符串 排序 日期 题目地址:https: ...

  8. 【LeetCode】1221. Split a String in Balanced Strings 解题报告 (C++)

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

  9. 【LeetCode】604. Design Compressed String Iterator 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护当前字符和次数 日期 题目地址:https://l ...

随机推荐

  1. 27 August

    高精度 struct bigint{ int a[1000],an; bigint operator = (int b){ an=0; while (b){a[an++]=b%10;b/=10;} r ...

  2. Spring JDBCTemplate 简单使用

    Spring JDBCTemplate applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8&q ...

  3. python中生成器generator

    通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大多数元素 ...

  4. Spring cloud gateway自定义filter以及负载均衡

    自定义全局filter package com.example.demo; import java.nio.charset.StandardCharsets; import org.apache.co ...

  5. PTA 1121 Damn Single

    题目链接:1121 Damn Single (25 分) "Damn Single (单身狗)" is the Chinese nickname for someone who i ...

  6. 字符串模式匹配算法系列(二):KMP算法

    算法背景: KMP算法是由Donald Knuth和Vaughan Pratt于1970年共同提出的,而James H.Morris也几乎同时间独立提出了这个算法.因此人们将其称作“克努特-莫里斯-普 ...

  7. jQuery基础--动画操作

    三组基本动画 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=" ...

  8. spring-第十九篇AOP面向切面编程之增强处理的优先级

    1.从我们第十七篇举例了不同类型的增强处理. spring AOP采用和AspectJ一样的优先顺序来织入增强处理:在“进入”连接点时,具有最高优先级的增强处理将先被织入(在给定的两个Before增强 ...

  9. 解决Linux下编译.sh文件报错 “[: XXXX: unexpected operator”

    本人经常在Linux通过编译 .sh文件来生成工程,之前一直都没问题,代码一直都没变,但是今天编译的时候,却提示错误:

  10. Day7-----Python的序列类(有子类:元组类,列表类)

    序列类型 1.基本介绍: 序列类型是一种基类类型  ,既然被称为那就肯定是有道理的,关于序列 它有  正向  和  反向  两种序号,正向序号从零开始,反向序号从负一开始 a = '例如这个字符串' ...