Skip to content

Commit 2665132

Browse files
add solution for 1358
1 parent 56cffba commit 2665132

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/main/java/com/fishercoder/solutions/_1358.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.fishercoder.solutions;
22

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+
39
/**
410
* 1358. Number of Substrings Containing All Three Characters
511
*
@@ -26,9 +32,23 @@
2632
* */
2733
public class _1358 {
2834
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+
* */
2938
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;
3251
}
52+
3353
}
3454
}

0 commit comments

Comments
 (0)