Number of Substrings Containing All Three Characters
Medium · Hash Table, String, Sliding Window
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" Output: 10
The substrings containing at least one occurrence of the characters a, b and c are "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc" and "abc" (again).
Example 2
Input: s = "aaacb" Output: 3
The substrings containing at least one occurrence of the characters a, b and c are "aaacb", "aacb" and "acb".
Example 3
Input: s = "abc" Output: 1
Constraints
- 3 <= s.length <= 5 x 10^4
- s only consists of a, b or c characters.
Starter code (python)
class Solution(object):
def numberOfSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
Related problems
- Two Sum (Easy)
- Longest Substring Without Repeating Characters (Medium)
- Longest Palindromic Substring (Medium)
Previous: Sort Integers by The Number of 1 Bits · Next: Count All Valid Pickup and Delivery Options · All problems
Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.