Number of Substrings Containing All Three Characters LeetCode solution in C++
1class Solution {2public:3int numberOfSubstrings(string s) {4int i = 0, j = 0;5int n = s.size();6map<char, int> mp;7int count = 0;8while (j < n) {9mp[s[j]]++;10if (mp.size() < 3) {11j++;12} else {13while (mp.size() == 3) {14count += (n - j);15mp[s[i]]--;16if (mp[s[i]] == 0) mp.erase(s[i]);17i++;18}19j++;20}21}22return count;23}24};