File tree Expand file tree Collapse file tree 2 files changed +42
-4
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +42
-4
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
+ import java .util .ArrayList ;
4
+ import java .util .Collections ;
5
+ import java .util .HashMap ;
6
+ import java .util .List ;
7
+ import java .util .Map ;
8
+
3
9
/**
4
10
* 1358. Number of Substrings Containing All Three Characters
5
11
*
26
32
* */
27
33
public class _1358 {
28
34
public static class Solution1 {
35
+ /**A classic sliding window problem, no dp or backtracking, just sliding window: use two pointers.
36
+ * my new favorite queustion!
37
+ * */
29
38
public int numberOfSubstrings (String s ) {
30
- //TODO: implement it
31
- return -1 ;
39
+ int [] counts = new int [3 ];
40
+ int i = 0 ;
41
+ int n = s .length ();
42
+ int result = 0 ;
43
+ for (int j = 0 ; j < n ; j ++) {
44
+ counts [s .charAt (j ) - 'a' ]++;
45
+ while (counts [0 ] > 0 && counts [1 ] > 0 && counts [2 ] > 0 ) {
46
+ counts [s .charAt (i ++) - 'a' ]--;
47
+ }
48
+ result += i ;
49
+ }
50
+ return result ;
32
51
}
52
+
33
53
}
34
54
}
You can’t perform that action at this time.
0 commit comments