Two approaches. Beats 100.00%
ApproachApproach 1: Sliding Window
Approach 2: Last Position TrackingComplexity
Time complexity: O(n)

Space complexity: O(1)
Code JS/TSCode C/C++
# Approach
Approach 1: Sliding Window
Approach 2: Last Position Tracking

![2.png](https://assets.leetcode.com/users/images/3db30705-a182-4e2e-9996-b113713580d5_1741711001.318233.png)

![1.png](https://assets.leetcode.com/users/images/99702b62-9513-4c46-9b01-a81f159c7389_1741711072.5647237.png)

# Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(1)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code JS/TS
```javascript [Sliding Window]
const numberOfSubstrings = (a) => {
    const vow = new Uint16Array(3);
    let vowCount = 0;
    const add = (i) => {
        vowCount += vow[i] === 0;
        vow[i] += 1;
        return vowCount === 3;
    };
    const del = (i) => {
        vowCount -= vow[i] === 1;
        vow[i] -= 1;
        return vowCount === 3;
    };
    const { length } = a;
    let result = 0;
    for (let i = 0, j = 0; j < length; ++j) {
        if (add(a.charCodeAt(j) - 97)) {
            result += length - j;
            while (del(a.charCodeAt(i++) - 97)) {
                result += length - j;
            }
        }
    }
    return result;
}
```
```javascript [Last Position Tracking]
const numberOfSubstrings = (a) => {
    const vow = new Int32Array(3);
    vow[0] = vow[1] = vow[2] = -1;
    const n = a.length;
    let result = 0;
    for (let i = 0; i < n; ++i) {
        vow[a.charCodeAt(i) - 97] = i;
        result += Math.min(vow[0], vow[1], vow[2]) + 1;
    }
    return result;
}
```
# Code C/C++
```cpp [Last Position Tracking]
class Solution {
public:
    int numberOfSubstrings(string a) {
        int vow[] = {-1, -1, -1};
        int result = 0;
        for (int i = 0; i < a.size(); ++i) {
            vow[a[i] - 97] = i;
            result += min(vow[0], min(vow[1], vow[2])) + 1;
        }
        return result;
    }
};
```
['Hash Table', 'String', 'C', 'Sliding Window', 'C++', 'TypeScript', 'JavaScript']