From c4dfb6db4f4375729b28bd39fc90cb385b482b3d Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Mon, 10 Mar 2025 18:20:28 -0600 Subject: 1358. number of substrings containing all three characters --- num_substrings_3_char.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 num_substrings_3_char.py (limited to 'num_substrings_3_char.py') diff --git a/num_substrings_3_char.py b/num_substrings_3_char.py new file mode 100644 index 0000000..13da3c4 --- /dev/null +++ b/num_substrings_3_char.py @@ -0,0 +1,31 @@ +# 1358. Number of Substrings Containing All Three Characters + +""" +given a string 's' consisting only of characters 'a', 'b', and 'c'. return +the number of substrings containing at least one occurence of all these +characters 'a', 'b', and 'c'. +""" + + +class Solution(object): + def numberOfSubstrings(self, s): + """ + :type s: str + :rtype: int + """ + cnt = {c: 0 for c in "abc"} + ans, i = 0, 0 + for j in range(len(s)): + cnt[s[j]] += 1 + while all(cnt.values()): + cnt[s[i]] -= 1 + i += 1 + ans += i + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.numberOfSubstrings(s="abcabc")) + print(obj.numberOfSubstrings(s="aaacb")) + print(obj.numberOfSubstrings(s="abc")) -- cgit v1.2.3