Substrings of Size Three with Distinct Characters (LeetCode 1876): Solution and Explanation
Count the substrings of length three whose characters are all different. A window of fixed size three slides across the string, and each window needs only three comparisons.
Intuition
A substring of length three is fully described by its start index, so there are exactly n - 2 candidates, one per position of a three-wide window.
Three characters are distinct exactly when no pair matches. There are only three pairs: first with second, second with third, and first with third.
Forgetting the first-with-third pair is the classic bug: in "aba" the neighbours differ, yet the window is not good.
Approach: Fixed window of three
- Start
countat 0. - For each
ifrom 0 ton - 3, reada = s[i],b = s[i + 1],c = s[i + 2]. - If
a != b,b != canda != c, incrementcount. - Return
count.
Step-by-step example
s = "xyzzaz"
- Window 0..2 is "xyz": all pairs differ, count = 1.
- Window 1..3 is "yzz": b = c = 'z', skip.
- Window 2..4 is "zza": a = b = 'z', skip.
- Window 3..5 is "zaz": a = c = 'z', skip.
- Answer: 1.
Code: Python, Java, C++
1class Solution:
2 def countGoodSubstrings(self, s: str) -> int:
3 count = 0
4 for i in range(len(s) - 2):
5 a, b, c = s[i], s[i + 1], s[i + 2]
6 if a != b and b != c and a != c:
7 count += 1
8 return count1class Solution {
2 public int countGoodSubstrings(String s) {
3 int count = 0;
4 for (int i = 0; i + 2 < s.length(); i++) {
5 char a = s.charAt(i), b = s.charAt(i + 1), c = s.charAt(i + 2);
6 if (a != b && b != c && a != c) {
7 count++;
8 }
9 }
10 return count;
11 }
12}1class Solution {
2public:
3 int countGoodSubstrings(string s) {
4 int count = 0;
5 for (int i = 0; i + 2 < (int)s.size(); i++) {
6 char a = s[i], b = s[i + 1], c = s[i + 2];
7 if (a != b && b != c && a != c) {
8 count++;
9 }
10 }
11 return count;
12 }
13};Time and space complexity
O(n)
One pass with three constant-time comparisons per window.
O(1)
Only the counter and three characters.
Other approaches
Set size check (time O(n), space O(1))
Put the three characters of each window into a set and count the window if the set has size 3. Clear and general, with a small constant overhead.
Sliding frequency counter (time O(n), space O(1))
Keep counts for the window and the number of characters whose count exceeds 1, updating on each slide. This is the pattern that generalises to windows of size k.
Common mistakes
- Checking only neighbouring characters and missing
s[i] == s[i + 2], as in "aba". - Looping to
n - 1and reading past the end of the string. - Counting distinct good substrings instead of occurrences. "abc" appearing twice counts twice.
- Not handling strings shorter than three, where the loop must not run at all.
Frequently asked questions
Do repeated occurrences of the same substring count separately?
Yes. The task counts positions, so every window that passes is counted, even if its text appeared earlier.
What if the string has fewer than three characters?
No window of length three fits, so the answer is 0.
How would this change for windows of size k?
Keep a frequency map for the window and a count of repeated characters. Each slide adds one character and removes one, and a window is good when no character repeats.
What is the time and space complexity?
O(n) time and O(1) extra space.