Skip to content

Commit cf0b4cd

Browse files
solves substrings with distinct characters
1 parent 7be03ee commit cf0b4cd

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@
453453
| 1859 | [Sorting the Sentence](https://leetcode.com/problems/sorting-the-sentence) | [![Java](assets/java.png)](src/SortingTheSentence.java) | |
454454
| 1863 | [Sum of All Subset XOR Totals](https://leetcode.com/problems/sum-of-all-subset-xor-totals) | [![Java](assets/java.png)](src/SumOfAllSubsetXORTotals.java) | |
455455
| 1869 | [Longer Contiguous Segments of Ones than Zeros](https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros) | [![Java](assets/java.png)](src/LongerContiguousSegmentOfOnesThanZeros.java) | |
456-
| 1876 | [Substrings of Size Three with Distinct Characters](https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters) | | |
456+
| 1876 | [Substrings of Size Three with Distinct Characters](https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters) | [![Java](assets/java.png)](src/SubstringsOfSizeThreeWithDistinctCharacters.java) | |
457457
| 1880 | [Check if Word Equals Summation of Two Words](https://leetcode.com/problems/check-if-word-equals-summation-of-two-words) | | |
458458
| 1886 | [Determine Whether Matrix Can Be Obtained By Rotation](https://leetcode.com/problems/determine-whether-matrix-can-be-obtained-by-rotation) | | |
459459
| 1893 | [Check if All the Integers in a Range Are Covered](https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered) | | |

Diff for: src/SubstringsOfSizeThreeWithDistinctCharacters.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters
2+
// T: O(N)
3+
// S: O(1)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class SubstringsOfSizeThreeWithDistinctCharacters {
9+
public int countGoodSubstrings(String s) {
10+
if (s.length() < 3) return 0;
11+
final Map<Character, Integer> frequency = getFrequency(s, 3);
12+
int goodSubStrings = 0;
13+
if (frequency.size() == 3) goodSubStrings++;
14+
for (int index = 3 ; index < s.length() ; index++) {
15+
remove(frequency, s.charAt(index - 3));
16+
frequency.put(s.charAt(index), frequency.getOrDefault(s.charAt(index), 0) + 1);
17+
if (frequency.size() == 3) goodSubStrings++;
18+
}
19+
return goodSubStrings;
20+
}
21+
22+
private Map<Character, Integer> getFrequency(String s, int upto) {
23+
final Map<Character, Integer> frequencies = new HashMap<>();
24+
for (int i = 0 ; i < upto ; i++) {
25+
frequencies.put(s.charAt(i), frequencies.getOrDefault(s.charAt(i), 0) + 1);
26+
}
27+
return frequencies;
28+
}
29+
30+
private void remove(Map<Character, Integer> frequencies, char c) {
31+
if (frequencies.get(c) == 1) frequencies.remove(c);
32+
else frequencies.put(c, frequencies.get(c) - 1);
33+
}
34+
}

0 commit comments

Comments
 (0)