✅Strings|🧑‍💻 BEGINNER FREINDLY|🌟Visualization|JAVA|C++|Python
Approach💡1.Use a fixed-size array (count) to track the occurrences of a, b, and c.
2 . Use a sliding window with two pointers (left and i) to expand and contrac

# Approach💡
<!-- Describe your approach to solving the problem. -->
1.Use a fixed-size array (```count```) to track the occurrences of ```a```, ```b```, and ```c```.
2 . Use a sliding window with two pointers (```left``` and ```i```) to expand and contract the window.
3 . For each window that contains all three characters (```a```, ```b```, and ```c```), count all valid substrings ending at or beyond the current position.
4 . Move the ```left``` pointer to shrink the window and repeat the process.
___

# ⏳Complexity Analysis
- Time complexity:$$O(n) $$Each character is processed at most twice (once by i and once by left).
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:$$O(1) $$Uses a fixed-size array of size 3.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
___


### Step-by-Step Visualization🔍:
```Example: s = "abcabc"```


![image.png](https://assets.leetcode.com/users/images/13a5996c-0ba2-4e1b-b1da-62742840549a_1741654485.6879225.png)
![image.png](https://assets.leetcode.com/users/images/b7ff2804-6933-4557-8cec-8d6158e6d2db_1741654521.3715124.png)
![image.png](https://assets.leetcode.com/users/images/8b1ce50b-d887-4ee3-b945-1ba2a5df42a8_1741654560.1499925.png)
![image.png](https://assets.leetcode.com/users/images/69bf6d93-4d6f-4ed4-bf71-120688e421ae_1741654578.7767591.png)
![image.png](https://assets.leetcode.com/users/images/2401603c-0721-46b3-82c7-d3e854505228_1741654596.1142974.png)

![image.png](https://assets.leetcode.com/users/images/18afe025-8413-471c-ac0b-eebea9ff5c6b_1741654641.8350945.png)
____








### 💻Code Implementation
```java []
class Solution {
    public int numberOfSubstrings(String s) { 
    int[] count = new int[3]; 
        int left = 0; 
        int result = 0;
        for (int i = 0; i < s.length(); i++) {
            count[s.charAt(i) - 'a']++;
  
            while (count[0] > 0 && count[1] > 0 && count[2] > 0) {
                result += s.length() - i;
                count[s.charAt(left) - 'a']--; 
                left++; 
            }
        }
        return result; 
    }
}
```
```c++ []
class Solution {
public:
    int numberOfSubstrings(std::string s) {
        std::vector<int> count(3, 0); 
        int left = 0; 
        int result = 0; 
        for (int i = 0; i < s.length(); i++) {
            count[s[i] - 'a']++; 

            while (count[0] > 0 && count[1] > 0 && count[2] > 0) {
                result += s.length() - i; 
                count[s[left] - 'a']--; 
                left++; 
            }
        }
        return result; 
    }
};
```
```python []
class Solution(object):
    def numberOfSubstrings(self, s):
        count = [0] * 3  
        left = 0  
        result = 0  
        for i in range(len(s)):
            count[ord(s[i]) - ord('a')] += 1  

            while count[0] > 0 and count[1] > 0 and count[2] > 0:
                result += len(s) - i  
                count[ord(s[left]) - ord('a')] -= 1  
                left += 1 
        return result
```

```javaScript []
function numberOfSubstrings(s) {
    let count = [0, 0, 0]; 
    let left = 0; 
    let result = 0; 

    for (let i = 0; i < s.length; i++) {
        count[s.charCodeAt(i) - 'a'.charCodeAt(0)]++; 

        while (count[0] > 0 && count[1] > 0 && count[2] > 0) {
            result += s.length - i; 
            count[s.charCodeAt(left) - 'a'.charCodeAt(0)]--; 
            left++; 
        }
    }

    return result; 
}
```
```c []
int numberOfSubstrings(char* s) {
    int count[3] = {0};
    int left = 0;      
    int result = 0;    
    int length = strlen(s); 

    for (int i = 0; i < length; i++) {
        count[s[i] - 'a']++; 

        while (count[0] > 0 && count[1] > 0 && count[2] > 0) {
            result += length - i;
            count[s[left] - 'a']--; 
            left++; 
        }
    }
    return result; 
}

```
___
### **Why This Method is Efficient :**
- The algorithm runs in O(n) time, where ```n``` is the length of the string ```s```.
- Each character is processed at most twice (once when expanding the window and once when contracting it).
___
**If you found my solution helpful, I would greatly appreciate your upvote, as it would motivate me to continue sharing more solutions.**

🔼 **Please Upvote**
🔼 **Please Upvote**
🔼 **Please Upvote**
      
![fabef4f9-4e9b-49bb-b2a4-67e31a893502_1728361136.9872687.jpeg](https://assets.leetcode.com/users/images/69d4e519-9168-4bb4-bea4-74bebecda8e5_1741653550.6173458.jpeg)

['String', 'C', 'Sliding Window', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']