题目描述
把字符串 s 看作是 “abcdefghijklmnopqrstuvwxyz” 的无限环绕字符串,所以 s 看起来是这样的:
"...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd...." .
现在给定另一个字符串 p 。返回 s 中 唯一 的 p 的 非空子串 的数量 。
示例 1:
输入: p = "a"
输出: 1
解释: 字符串 s 中只有一个"a"子字符。
示例 2:
输入: p = "cac"
输出: 2
解释: 字符串 s 中的字符串“cac”只有两个子串“a”、“c”。.
示例 3:
输入: p = "zab"
输出: 6
解释: 在字符串 s 中有六个子串“z”、“a”、“b”、“za”、“ab”、“zab”。
提示:
1 <= p.length <= 105
p 由小写英文字母构成
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/unique-substrings-in-wraparound-string
方法一:
动态规划,定义dp[i]表示以第i个字符结尾且在s中的子串的最长长度。
class Solution {
public:
int findSubstringInWraproundString(string p) {
vector<int> dp(26);
int k=1,sum=0;
for(int i=0;i<p.length();i++){
if(i&&(p[i]-p[i-1]+26)%26==1){
k++;
}
else{
k=1;
}
dp[p[i]-'a']=max(dp[p[i]-'a'],k);
}
for(int a:dp){
sum+=a;
}
return sum;
}
};