【leetcode】828. Unique Letter String
题目如下:
A character is unique in string
Sif 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 stringS.For example,
UNIQ("LETTER") = 2.Given a string
Swith only uppercases, calculate the sum ofUNIQ(substring)over all non-empty substrings ofS.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 = 10Example 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的更多相关文章
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- [LeetCode] 828. Unique Letter String 独特字符串
A character is unique in string S if it occurs exactly once in it. For example, in string S = " ...
- 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...
- 【LeetCode】467. Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 【Leetcode】804. Unique Morse Code Words
Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...
- 【LeetCode】791. Custom Sort String 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 按顺序构造字符串 排序 日期 题目地址:https: ...
- 【LeetCode】1221. Split a String in Balanced Strings 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 日期 题目地址:https://leetcode ...
- 【LeetCode】604. Design Compressed String Iterator 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护当前字符和次数 日期 题目地址:https://l ...
随机推荐
- MyView.java 自己画的view
package myapplication21.lum.com.mycanvas; import android.content.Context;import android.graphics.Can ...
- codeforces 617 E. XOR and Favorite Number(莫队算法)
题目链接:http://codeforces.com/problemset/problem/617/E 题目: 给你a1 a2 a3 ··· an 个数,m次询问:在[L, R] 里面又多少中 [l, ...
- MongoDB性能分析
# mongostat --host=100.150.2.12 --port=27017 --authenticationDatabase=admin --username=root --passwo ...
- Vagrant - 打造跨平台的一致开发环境
官网 参考资料 借助 Vagrant ,可以使用 Vagrantfile 文件自动化虚拟机的安装和配置流程,方便快速的打造跨平台的统一开发环境. 1. Vagrant 是啥 Vagrant 用于构建及 ...
- javascript获取select 的id与值
javascript获取select 的id与值 <script type="text/javascript"> function showOptionId () { ...
- JS基础(上)
JS与DOM的关系 浏览器有渲染html代码的功能,把html源码(如div,p标签等)在内存里形成一个DOM对象 文档对象模型DOM(Document Object Model)定义访问和处理HTM ...
- 【ABAP系列】SAP ABAP基础-abap数据类型的解析整理
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP基础-abap数 ...
- Maven入门学习 (一)
学习Java 的同学就一定会学习Maven, 那么Maven为什么会得到大量用户的使用呢?它是用来干什么的呢 ?接下来就来介绍 Q:Maven的作用? (1)Maven可以自动化构建项目,可以从清理. ...
- IOS-问题整理
安装包相关 安装包无法打开 通用中点击验证应用无反应 卸载-删除证书-重装
- C#递归加载目录树
/// 获取目录管理信息集合 /// </summary> /// <returns></returns> public List<CatalogTree&g ...