Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2629def

Browse files
committed
Add solution #1358
1 parent 8928509 commit 2629def

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 1358. Number of Substrings Containing All Three Characters
3+
* https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/
4+
* Difficulty: Medium
5+
*
6+
* Given a string s consisting only of characters a, b and c.
7+
*
8+
* Return the number of substrings containing at least one occurrence of all these
9+
* characters a, b and c.
10+
*/
11+
12+
/**
13+
* @param {string} s
14+
* @return {number}
15+
*/
16+
var numberOfSubstrings = function(s) {
17+
const count = [0, 0, 0];
18+
let result = 0;
19+
let left = 0;
20+
21+
for (let right = 0; right < s.length; right++) {
22+
count[s[right].charCodeAt(0) - 97]++;
23+
24+
while (count[0] > 0 && count[1] > 0 && count[2] > 0) {
25+
result += s.length - right;
26+
count[s[left].charCodeAt(0) - 97]--;
27+
left++;
28+
}
29+
}
30+
31+
return result;
32+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@
653653
1351|[Count Negative Numbers in a Sorted Matrix](./1351-count-negative-numbers-in-a-sorted-matrix.js)|Easy|
654654
1352|[Product of the Last K Numbers](./1352-product-of-the-last-k-numbers.js)|Medium|
655655
1356|[Sort Integers by The Number of 1 Bits](./1356-sort-integers-by-the-number-of-1-bits.js)|Easy|
656+
1358|[Number of Substrings Containing All Three Characters](./1358-number-of-substrings-containing-all-three-characters.js)|Medium|
656657
1360|[Number of Days Between Two Dates](./1360-number-of-days-between-two-dates.js)|Easy|
657658
1365|[How Many Numbers Are Smaller Than the Current Number](./1365-how-many-numbers-are-smaller-than-the-current-number.js)|Easy|
658659
1366|[Rank Teams by Votes](./1366-rank-teams-by-votes.js)|Medium|

0 commit comments

Comments
 (0)