LeetCdoe 1358. Number of Substrings Containing All Three Characters
Question
Given a string s
consisting only of characters a, b and c.
Return the number of substrings containing at least one occurrence of all these characters a, b and c.
Example
1 | Input: s = "abcabc" |
Solution
It’s a sliding window question, but how to count the qualified string is the key. If we meet the requirement, the number of it would be n - end
, which stands for string ends from end pointer to length of string. For example,abcabc
, when we go the first char c, the number should be abc, abca, abcab, abcabc; total = 4
. Also, we use count array to record if it meets the requirement.
Code
1 | // time:O(n), space:O(1) |