自学内容网 自学内容网

C语言 | Leetcode C语言题解之第467题环绕字符串中唯一的子字符串

题目:

题解:

#define MAX(a, b) ((a) > (b) ? (a) : (b))

int findSubstringInWraproundString(char * p) {
    int dp[26];
    int len = strlen(p);
    memset(dp, 0, sizeof(dp));
    int k = 0;
    for (int i = 0; i < len; ++i) {
        if (i && (p[i] - p[i - 1] + 26) % 26 == 1) { // 字符之差为 1 或 -25
            ++k;
        } else {
            k = 1;
        }
        dp[p[i] - 'a'] = MAX(dp[p[i] - 'a'], k);
    }
    int res = 0;
    for (int i = 0; i < 26; i++) {
        res += dp[i];
    }
    return res;
}

原文地址:https://blog.csdn.net/m0_59237910/article/details/142800835

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!