Bootstrap

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;
}
;