-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC1876_SubstringsOfSizeThreeWithDistinctCharacters.java
More file actions
45 lines (41 loc) · 1.36 KB
/
LC1876_SubstringsOfSizeThreeWithDistinctCharacters.java
File metadata and controls
45 lines (41 loc) · 1.36 KB
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
package practise.src.main.java.leetCode;
import java.util.HashMap;
public class LC1876_SubstringsOfSizeThreeWithDistinctCharacters {
public static void main(String[] args) {
String s = "aababcabc";
System.out.println(countGoodSubstrings(s));
}
private static int countGoodSubstrings(String s) {
// int n = s.length();
// if(n < 3) return 0;
// HashMap<Character, Integer> map = new HashMap<>();
//
// int left = 0, count = 0;
// for(int right = 0; right < n; right++) {
// map.put(s.charAt(right), map.getOrDefault(s.charAt(right), 0) + 1);
//
// //when window size is = 3
// if(right - left + 1 == 3) {
// if(map.size() == 3) count++;
// int freq = map.get(s.charAt(left));
// if(freq == 1){
// map.remove(s.charAt(left));
// } else {
// map.put(s.charAt(left), freq - 1);
// }
// left++;
// }
// }
// return count;
int count=0;
for(int i=0; i<=s.length()-3; i++){
char a=s.charAt(i);
char b=s.charAt(i+1);
char c=s.charAt(i+2);
if(a!=b && b!=c && a!=c){
count++;
}
}
return count;
}
}