🚀Using Hashmap || 10 Lines C++
🚀 Approach: Sliding Window + Hashmap
We need to count substrings containing at least one of each character (a, b, c).
A sliding window technique with a hashmap
# 🚀 Approach: Sliding Window + Hashmap

- We need to **count substrings** containing **at least one of each character (`a`, `b`, `c`)**.  
A **sliding window** technique with a **hashmap** can solve this efficiently in $$O(n)$$.

---

# 💡 **Logic Breakdown**
1. **Expand the window** from the right (`i`).  
2. **Track the frequency** of each character using a hashmap.  
3. **Once all three characters are present** (`freq.size() == 3`):  
   - **All substrings** starting from `left` to `s.size()-1` will be valid.  
   - So, directly add `(s.size() - i)` to the count.  
4. **Shrink the window from the left** (`left++`) until the window becomes invalid.  
5. Repeat until the string is fully traversed.

---

# 💻 **Code** (Concise Version After This)
```cpp []
class Solution {
public:
    int numberOfSubstrings(string s) {
        unordered_map<char, int> freq;
        int left = 0, cnt = 0;
        for (int i = 0; i < s.size(); i++) {
            freq[s[i]]++;
            while (freq.size() == 3) {
                cnt += s.size() - i;
                freq[s[left]]--;
                if (freq[s[left]] == 0) freq.erase(s[left]);
                left++;
            }
        }
        return cnt;
    }
};
```
```Python []
class Solution:
    def numberOfSubstrings(self, s: str) -> int:
        freq = {}
        left = 0
        cnt = 0
        
        for i in range(len(s)):
            freq[s[i]] = freq.get(s[i], 0) + 1
            
            while len(freq) == 3:
                cnt += len(s) - i
                freq[s[left]] -= 1
                if freq[s[left]] == 0:
                    del freq[s[left]]
                left += 1
        
        return cnt
```
```Java []
class Solution {
    public int numberOfSubstrings(String s) {
        HashMap<Character, Integer> freq = new HashMap<>();
        int left = 0, cnt = 0;
        
        for (int i = 0; i < s.length(); i++) {
            freq.put(s.charAt(i), freq.getOrDefault(s.charAt(i), 0) + 1);
            
            while (freq.size() == 3) {
                cnt += s.length() - i;
                freq.put(s.charAt(left), freq.get(s.charAt(left)) - 1);
                if (freq.get(s.charAt(left)) == 0) {
                    freq.remove(s.charAt(left));
                }
                left++;
            }
        }
        
        return cnt;
    }
}
```
---
# 💻 Concise Version
```cpp []
class Solution {
public:
    int numberOfSubstrings(string s) {
        unordered_map<char, int> freq;
        int left = 0, cnt = 0;
        for (int i = 0; i < s.size(); i++) {
            freq[s[i]]++;
            while (freq.size() == 3) 
                cnt += s.size() - i, freq[s[left]]--, 
                if (!freq[s[left]]) freq.erase(s[left]), left++;
        }
        return cnt;
    }
};
```
['Hash Table', 'Python', 'C++', 'Java']