Skip to content

Commit 40e677e

Browse files
authored
Create Substrings of Size Three with Distinct Characters.java
1 parent bc1ddf9 commit 40e677e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int countGoodSubstrings(String s) {
3+
Map<Character, Integer> map = new HashMap<>();
4+
int count = 0;
5+
for (int i = 0; i < Math.min(s.length(), 2); i++) {
6+
map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
7+
}
8+
for (int i = 2; i < s.length(); i++) {
9+
map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
10+
if (map.size() == 3) {
11+
count++;
12+
}
13+
map.put(s.charAt(i - 2), map.getOrDefault(s.charAt(i - 2), 0) - 1);
14+
if (map.get(s.charAt(i - 2)) == 0) {
15+
map.remove(s.charAt(i - 2));
16+
}
17+
}
18+
return count;
19+
}
20+
}

0 commit comments

Comments
 (0)