17.Number of Substrings Containing All Three Characters
Input: s = "abcabc"
Output: 10
Explanation: The substrings containing at least one occurrence of the characters a, b and c are "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc" and "abc" (again). Input: s = "aaacb"
Output: 3
Explanation: The substrings containing at least one occurrence of the characters a, b and c are "aaacb", "aacb" and "acb". Input: s = "abc"
Output: 1Solution: (Sliding Window)
class Solution
{
public:
int numberOfSubstrings(string s)
{
vector<int> v{-1, -1, -1};
int res = 0;
for (int i = 0; i < s.length(); i++)
{
v[s[i] - 97] = i;
res = res + 1 + min(v[0], min(v[1], v[2]));
}
return res;
}
};Previous16. Binary Subarrays With SumNext18. Find Two Non-overlapping Sub-arrays Each With Target Sum
Last updated