forked from anishLearnsToCode/leetcode-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubstringsOfSizeThreeWithDistinctCharacters.java
34 lines (30 loc) · 1.25 KB
/
SubstringsOfSizeThreeWithDistinctCharacters.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
// https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters
// T: O(N)
// S: O(1)
import java.util.HashMap;
import java.util.Map;
public class SubstringsOfSizeThreeWithDistinctCharacters {
public int countGoodSubstrings(String s) {
if (s.length() < 3) return 0;
final Map<Character, Integer> frequency = getFrequency(s, 3);
int goodSubStrings = 0;
if (frequency.size() == 3) goodSubStrings++;
for (int index = 3 ; index < s.length() ; index++) {
remove(frequency, s.charAt(index - 3));
frequency.put(s.charAt(index), frequency.getOrDefault(s.charAt(index), 0) + 1);
if (frequency.size() == 3) goodSubStrings++;
}
return goodSubStrings;
}
private Map<Character, Integer> getFrequency(String s, int upto) {
final Map<Character, Integer> frequencies = new HashMap<>();
for (int i = 0 ; i < upto ; i++) {
frequencies.put(s.charAt(i), frequencies.getOrDefault(s.charAt(i), 0) + 1);
}
return frequencies;
}
private void remove(Map<Character, Integer> frequencies, char c) {
if (frequencies.get(c) == 1) frequencies.remove(c);
else frequencies.put(c, frequencies.get(c) - 1);
}
}