forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1358.java
48 lines (46 loc) · 1.51 KB
/
_1358.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.fishercoder.solutions;
/**
* 1358. Number of Substrings Containing All Three Characters
*
* Given a string s consisting only of characters a, b and c.
* Return the number of substrings containing at least one occurrence of all these characters a, b and c.
*
* Example 1:
* Input: s = "abcabc"
* Output: 10
* Explanation: The substrings containing at least one occurrence of the characters a, b and c are "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc" and "abc" (again).
*
* Example 2:
* Input: s = "aaacb"
* Output: 3
* Explanation: The substrings containing at least one occurrence of the characters a, b and c are "aaacb", "aacb" and "acb".
*
* Example 3:
* Input: s = "abc"
* Output: 1
*
* Constraints:
* 3 <= s.length <= 5 x 10^4
* s only consists of a, b or c characters.
* */
public class _1358 {
public static class Solution1 {
/**A classic sliding window problem, no dp or backtracking, just sliding window: use two pointers.
* my new favorite queustion!
* */
public int numberOfSubstrings(String s) {
int[] counts = new int[3];
int i = 0;
int n = s.length();
int result = 0;
for (int j = 0; j < n; j++) {
counts[s.charAt(j) - 'a']++;
while (counts[0] > 0 && counts[1] > 0 && counts[2] > 0) {
counts[s.charAt(i++) - 'a']--;
}
result += i;
}
return result;
}
}
}