[translate by ChatGPT] This problem is almost the same as the one from yesterday - it’s about finding substrings that contain characters at least a certain number of times. A two-pointer approach is sufficient to solve it successfully.

Code

class Solution {
public:
    int numberOfSubstrings(string s) {
        int n = s.size();
        vector<int> cnt(3, 0);
        int left = 0, right = 0, ans = 0;
        while(right < n) {
            ++cnt[s[right]-'a'];
            while(cnt[0] >= 1 && cnt[1] >= 1 && cnt[2] >= 1) {
                ans += (n - right);
                --cnt[s[left]-'a'];
                ++left;
            }
            ++right;
        }
        return ans;
    }
};