Leetcode 467 Solution

This article provides solution to leetcode question 467 (unique-substrings-in-wraparound-string)

https://leetcode.com/problems/unique-substrings-in-wraparound-string

Solution

class Solution {
public:
    int findSubstringInWraproundString(string p) {
        vector<int> cnt(26);
        int last_cnt = 0;

        for (int i = 0; i < p.size(); i++)
        {
            int val = p[i] - 'a';

            if (i == 0)
                last_cnt = 1;
            else
            {
                int last_val = p[i - 1] - 'a';

                if ((last_val + 1) % 26 == val % 26)
                    last_cnt++;
                else
                    last_cnt = 1;
            }

            cnt[val] = max(cnt[val], last_cnt);
        }

        return accumulate(cnt.begin(), cnt.end(), 0);
    }
};