clueless coding // TODO: be smarter

LeetCode Unique Substrings in Wraparound String: CPP Solution



TL;DR Code Solution


int findSubstringInWraproundString(string p) {

    vector<int> counts(26);
    int maxLength = 0;
    for (int i = 0; i < p.length(); i++) {

        if (i > 0 && (p[i] - p[i - 1] == 1 || p[i - 1] - p[i] == 25)) {
            maxLength++;
        }
        else {
            maxLength = 1;
        }

        counts[p[i] - 'a'] = max(counts[p[i] - 'a'], maxLength);

    }

    int result = 0;
    for (int i = 0; i < counts.size(); i++) {
        result += counts[i];
    }
    return result;

}