- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1358.java
24 lines (23 loc) · 765 Bytes
/
_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
packagecom.fishercoder.solutions.secondthousand;
publicclass_1358 {
publicstaticclassSolution1 {
/*
* A classic sliding window problem, no dp or backtracking, just sliding window: use two pointers.
* my new favorite question!
*/
publicintnumberOfSubstrings(Strings) {
int[] counts = newint[3];
inti = 0;
intn = s.length();
intresult = 0;
for (intj = 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;
}
returnresult;
}
}
}