467.环绕字符串中唯一的子字符串
链接:467.环绕字符串中唯一的子字符串
难度:Medium
标签:字符串、动态规划
简介:现在给定另一个字符串 p 。返回 s 中 唯一 的 p 的 非空子串 的数量 。 。
题解 1 - cpp
- 编辑时间:2022-05-25
- 执行用时:8ms
- 内存消耗:8MB
- 编程语言:cpp
- 解法介绍:遍历,每次储存以当前值结尾的最长长度。
class Solution {
   public:
    int findSubstringInWraproundString(string p) {
        int n = p.size(), ans = 0;
        vector<int> dp(n);
        unordered_map<char, int> m;
        dp[0] = 1;
        m[p[0]] = 1;
        for (int i = 1; i < n; i++) {
            int next = p[i - 1] == 'z' ? 'a' : p[i - 1] + 1;
            if (next != p[i])
                dp[i] = 1;
            else
                dp[i] = dp[i - 1] + 1;
            m[p[i]] = max(m[p[i]], dp[i]);
        }
        for (auto& item : m) ans += item.second;
        return ans;
    }
};