Number of Substrings Containing All Three Characters LeetCode solution in C++

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

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0