aboutsummaryrefslogtreecommitdiff
path: root/num_substrings_3_char.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 18:20:28 -0600
committerJack Sangdahl <[email protected]>2025-03-10 18:20:28 -0600
commitc4dfb6db4f4375729b28bd39fc90cb385b482b3d (patch)
treefd0b4c93df92c7e1e1241c6165636f1b4bbc8913 /num_substrings_3_char.py
parent7c60dee83b436e8e8a667bf9f191beced8de4fc3 (diff)
downloadleetcode-c4dfb6db4f4375729b28bd39fc90cb385b482b3d.tar.gz
leetcode-c4dfb6db4f4375729b28bd39fc90cb385b482b3d.zip
1358. number of substrings containing all three characters
Diffstat (limited to 'num_substrings_3_char.py')
-rw-r--r--num_substrings_3_char.py31
1 files changed, 31 insertions, 0 deletions
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"))