题目:
把字符串 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]:以字符串p[i]结尾的连续字符串的非空子串长度增量为dp[i]。
当 p[i-1] == p[i],count++,与上一个字符构成连续时:此情况下最长字符串长度为 dp[p[i]-'a'] = max(dp[p[i]-'a'],count);
当 p[i-1]!=p[i],count = 1,与上一个字符不连续 时: 此情况下最长字符串长度为 dp[p[i]-'a'] = max(dp[p[i]-'a'],count);。
代码:
class Solution {
public:
int findSubstringInWraproundString(string p) {
vector<int> dp(26,0);
int count = 0;
for(int i =0; i<p.size(); i++) {
if(i > 0 && ((p[i-1]+1)%26 == p[i]%26))
count++;
else
count = 1;
dp[p[i]-'a'] = max(dp[p[i]-'a'],count);
}
return accumulate(dp.begin(),dp.end(),0);
}
};