【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 ...
随机推荐
- HTTP请求时候总是设置的两个参数ConnectionTimeOut和SocketTimeOut
在HTTP请求时候总是设置两个参数,就是连接超时和Socket超时 public static final String SO_TIMEOUT = "http.socket.timeout& ...
- BaseFragment 基类代码
public abstract class BaseFragment extends Fragment implements IBaseView { private List<BasePrese ...
- vs2019里没有linq to sql或EF工具,导致dbml或者edmx无法通过设计器浏览
点击:工具->获取工具或功能 选择需要安装的工具,然后点击底部的修改按钮就可以了,等待安装完成,如下图:
- JS-线程、事件循环、任务队列
JS 是单线程的,但是却能执行异步任务,这主要是因为 JS 中存在事件循环(Event Loop)和任务队列(Task Queue). 事件循环: JS 会创建一个类似于 while (true) 的 ...
- 获取小程序accessToken
private static String getAccessToken(){ String url = "https://api.weixin.qq.com/cgi-bin/token? ...
- 云计算openstack共享组件——Memcache 缓存系统
一.缓存系统 静态web页面: 1.工作流程: 在静态Web程序中,客户端使用Web浏览器(IE.FireFox等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Reques ...
- Go-Mutex互斥量
先来看一段go1.12.5中Mutex的源码: // Copyright 2009 The Go Authors. All rights reserved. // Use of this source ...
- [LeetCode] 287. Find the Duplicate Number(Floyd判圈算法)
传送门 Description Given an array nums containing n + 1 integers where each integer is between 1 and n ...
- Codeforces 1114B (贪心)
题面 传送门 分析 答案很好看出,显然是选最大的m*k个数 那么如何构造方案呢 我们把最大的m*k个数的位置标记为1,其他标记为0 从左到右维护一个ptr,记录有标记的数的个数,如果当前有m个有标记的 ...
- C. DZY Loves Sequences
C. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...