Advertisement

Number of Substrings Containing All Three Characters - LeetCode 1358 Solution

Number of Substrings Containing All Three Characters - Complete Solution Guide

Number of Substrings Containing All Three Characters is LeetCode problem 1358, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 Explanation: 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 Explanation: The substrings containing at

Detailed Explanation

The problem requires us to find the number of substrings in a given string `s` (consisting only of 'a', 'b', and 'c' characters) that contain at least one occurrence of each character ('a', 'b', and 'c'). The input is the string `s`, and the output is the count of such substrings. The string length is between 3 and 50,000.

Solution Approach

The solution uses a sliding window approach. We maintain a window defined by `left` and `right` pointers. We expand the window by moving the `right` pointer one step at a time, updating the counts of each character in the window. When the window contains at least one of each character 'a', 'b', and 'c', we shrink the window from the left by incrementing the `left` pointer. The number of valid substrings ending at the current `right` position is equal to the current `left` index. We add this value to the total count of valid substrings.

Step-by-Step Algorithm

  1. Step 1: Initialize a hash map (or array) to store the counts of characters 'a', 'b', and 'c'.
  2. Step 2: Initialize `left` pointer to 0 and `res` (result counter) to 0.
  3. Step 3: Iterate through the string using the `right` pointer.
  4. Step 4: Increment the count of the character at the `right` pointer in the hash map.
  5. Step 5: While the window contains at least one 'a', one 'b', and one 'c', decrement the count of the character at the `left` pointer and increment `left` pointer.
  6. Step 6: After shrinking the window as much as possible, increment `res` by `left`. This represents the number of valid substrings ending at the current `right` position.
  7. Step 7: Repeat steps 3-6 until the `right` pointer reaches the end of the string.
  8. Step 8: Return `res`.

Key Insights

  • Insight 1: The problem can be efficiently solved using the sliding window technique.
  • Insight 2: We only need to keep track of the counts of each character ('a', 'b', 'c') within the current window.
  • Insight 3: For each valid window (containing all three characters), any substring ending at the right boundary starting from the beginning of the string up to current left pointer position will contain all three characters

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Hash Table, String, Sliding Window.

Companies

Asked at: DE Shaw, PayPal.