【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 ...
随机推荐
- HDU6415 Rikka with Nash Equilibrium
HDU6415 Rikka with Nash Equilibrium 找规律 + 大数 由于规律会被取模破坏,所以用了java 找出规律的思路是: 对于一个n*m的矩阵构造,我先考虑n*1的构造,很 ...
- thinkphp 级联菜单实现
养殖场->栋舍级联菜单 //获取默认养殖场和栋舍信息 public function sbjr(){ $yzc_model=M("Yzc"); $list = $yzc_mo ...
- jmeter之cookies登录
现在很多网站的登录都要验证码了,验证码的值是动态的,值不易获取.使用jmeter测试一个需要登录的接口就有困难,这时候,我们就可以使用cookies管理器来记住这个登录信息. 目录 1.jmeter的 ...
- PHP Yii框架中使用smarty模板
第一种方法 按照YII系统的办法生成视图觉得有点麻烦,觉得用smarty更省事.尝试着把smarty模板加进来了. date_default_timezone_set("PRC") ...
- 使EasyUI的window、panel、dialog 不被拖出页面
function easyuiPanelOnMove(left, top) { var l = left; var t = top; if (l < 1) { l = 1; } ...
- 高级软件工程第二次作业:随机生成N个不重复的已解答完毕的数独棋盘
#include <stdio.h> #include "SuduCheck.h" ][],int i,int j,int k) //判断是否可以将第i行.第j列的数设 ...
- Pikachu漏洞练习平台实验——RCE(五)
1.概述 RCE(Remote Command/Code Execute) 给攻击者向后台服务器远程注入操作系统命令或者代码,从而控制后台系统. 远程系统命令执行一般出现这种漏洞,是因为应用系统从设计 ...
- JavaScript Tre
function BinarySearchTree() { var Node = function(key) { this.key = key; this.left = null; this.righ ...
- Chrome打开标签页预览
类似于Microsoft Edge浏览器上的标签页缩略图预览非常方便,其实现在谷歌浏览器正在测试相关的功能,如果想提前体验,就在地址栏输入"chrome://flags"并按下回车 ...
- Netty基础-BIO/NIO/AIO
同步阻塞IO(BIO): 我们熟知的Socket编程就是BIO,每个请求对应一个线程去处理.一个socket连接一个处理线程(这个线程负责这个Socket连接的一系列数据传输操作).阻塞的原因在于:操 ...