File tree
Expand file treeCollapse file tree1 file changed
+21
-0
lines changed Expand file treeCollapse file tree1 file changed
+21
-0
lines changed Original file line number | Diff line number | Diff line change |
---|
|
| 1 | +/** |
| 2 | +* @param {string} s |
| 3 | +* @return {number} |
| 4 | +*/ |
| 5 | +var numberOfSubstrings = function (s) { |
| 6 | +let count = { 'a': 0, 'b': 0, 'c': 0 }; // Track character count |
| 7 | +let left = 0, result = 0; |
| 8 | + |
| 9 | +for (let right = 0; right < s.length; right++) { |
| 10 | +count[s[right]]++; // Expand window |
| 11 | + |
| 12 | +// When all characters are in the window, move left pointer |
| 13 | +while (count['a'] > 0 && count['b'] > 0 && count['c'] > 0) { |
| 14 | +result += s.length - right; // All substrings from left to end are valid |
| 15 | +count[s[left]]--; |
| 16 | +left++; // Shrink window |
| 17 | +} |
| 18 | +} |
| 19 | + |
| 20 | +return result; |
| 21 | +}; |
You can’t perform that action at this time.
0 commit comments