Unique Letter String
A character is unique in stringS
if it occurs exactly once in it.
For example, in stringS = "LETTER"
, the only unique characters are"L"
and"R"
.
Let's defineUNIQ(S)
as the number of unique characters in stringS
.
For example,UNIQ("LETTER") = 2
.
Given a stringS
with only uppercases, calculate the sum ofUNIQ(substring)
over all non-empty substrings ofS
.
If there are two or more equal substrings at different positions inS
, we consider them different.
Since the answer can be very large, return the answer modulo 10 ^ 9 + 7
.
Example 1:
Example 2:
Note:0 <= S.length <= 10000
.
分析
比较Subarray Product Less Than K
动态规划
Take the below example:
When extendings[i]
to all substrings ending withs[i-1]
, there are(i-f)
more unique chars[i]
, and(f-s)
less unique char because of duplicate ofs[i]
.
每次到i位置,f-i段贡献了unique,但是f-s段重复了需要去掉。注意坐标
i-f几条线段,每段线段长度都加1,就是增量,还要加上Base=前面几段线段长度总和
Last updated