1358. Number of Substrings Containing All Three Characters
</> Solution
class Solution {
public int numberOfSubstrings(String s) {
int[] count = new int[3];
int left = 0;
int ans = 0;
for (int right = 0; right < s.length(); right++) {
count[s.charAt(right) - 'a']++;
while (count[0] > 0 && count[1] > 0 && count[2] > 0) {
ans += s.length() - right;
count[s.charAt(left) - 'a']--;
left++;
}
}
return ans;
}
}
class Solution:
def numberOfSubstrings(self, s: str) -> int:
count = [0] * 3
left = 0
ans = 0
for right in range(len(s)):
count[ord(s[right]) - ord('a')] += 1
while count[0] > 0 and count[1] > 0 and count[2] > 0:
ans += len(s) - right
count[ord(s[left]) - ord('a')] -= 1
left += 1
return ans
class Solution {
public:
int numberOfSubstrings(string s) {
vector<int> count(3, 0);
int left = 0;
int ans = 0;
for (int right = 0; right < s.length(); right++) {
count[s[right] - 'a']++;
while (count[0] > 0 && count[1] > 0 && count[2] > 0) {
ans += s.length() - right;
count[s[left] - 'a']--;
left++;
}
}
return ans;
}
};
/**
* @param {string} s
* @return {number}
*/
var numberOfSubstrings = function(s) {
const count = [0, 0, 0];
let left = 0;
let ans = 0;
for (let right = 0; right < s.length; right++) {
count[s.charCodeAt(right) - 97]++;
while (count[0] > 0 && count[1] > 0 && count[2] > 0) {
ans += s.length - right;
count[s.charCodeAt(left) - 97]--;
left++;
}
}
return ans;
};