LeetCode Problem Workspace
Substrings of Size Three with Distinct Characters
Count all substrings of length three in a string where each character is unique using sliding window techniques efficiently.
4
Topics
6
Code langs
3
Related
Practice Focus
Easy · Sliding window with running state updates
Answer-first summary
Count all substrings of length three in a string where each character is unique using sliding window techniques efficiently.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Sliding window with running state updates
Use a sliding window of size three to traverse the string while maintaining a set of characters for uniqueness. Increment a counter when all three characters in the window are distinct. This approach leverages constant-time set operations to ensure each substring is evaluated efficiently without redundant checks.
Problem Statement
Given a string s consisting of lowercase English letters, return the total number of substrings of length three where all characters are distinct. A substring is considered 'good' if it contains no repeated characters.
Each substring occurrence is counted separately, even if it appears multiple times in s. For example, s = "xyzzaz" has substrings "xyz", "yzz", "zza", and "zaz"; only "xyz" is a good substring.
Examples
Example 1
Input: s = "xyzzaz"
Output: 1
There are 4 substrings of size 3: "xyz", "yzz", "zza", and "zaz". The only good substring of length 3 is "xyz".
Example 2
Input: s = "aababcabc"
Output: 4
There are 7 substrings of size 3: "aab", "aba", "bab", "abc", "bca", "cab", and "abc". The good substrings are "abc", "bca", "cab", and "abc".
Constraints
- 1 <= s.length <= 100
- s consists of lowercase English letters.
Solution Approach
Sliding Window with Set
Maintain a window of size three and use a set to track characters. Move the window forward by one character each time, updating the set, and count windows where the set size is three.
Iterative Comparison
Loop through the string and directly compare the three characters in each window for uniqueness. Increment a counter when all three are distinct. This avoids extra data structures but still follows the sliding window pattern.
Optimized Counting with Hash Table
Use a hash table to track character counts in the current window. Slide the window while updating counts efficiently. When the counts indicate three unique characters, increment the result. This reduces repeated set creation.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(n) since each character is visited once in the sliding window. Space complexity is O(1) because the set or hash table only tracks at most three characters at a time.
What Interviewers Usually Probe
- Emphasis on sliding window to check all substrings of fixed size.
- Looking for use of a set or hash table to quickly detect distinct characters.
- Expect attention to counting every occurrence of a good substring without missing duplicates.
Common Pitfalls or Variants
Common pitfalls
- Counting only unique substrings instead of all occurrences.
- Forgetting to reset or update the set/hash table correctly when sliding the window.
- Comparing characters incorrectly and miscounting substrings with repeated letters.
Follow-up variants
- Count substrings of length k with distinct characters for any k > 1.
- Return all good substrings instead of just counting them.
- Check for substrings of size three that have exactly two distinct characters.
FAQ
What is a 'good' substring in Substrings of Size Three with Distinct Characters?
A 'good' substring has exactly three characters with no repetitions, meaning all characters are unique.
Can I use nested loops instead of a sliding window?
Yes, but a sliding window is more efficient for checking consecutive substrings of size three.
Does the order of characters matter?
Yes, the substring is contiguous in the original string, so the order matters for both uniqueness and counting.
How do I handle multiple occurrences of the same substring?
Each occurrence is counted separately; do not merge or remove duplicates when incrementing the result.
Is a set or hash table necessary for this sliding window problem?
Using a set or hash table simplifies checking for distinct characters and follows the sliding window pattern efficiently.
Solution
Solution 1: Sliding Window
We can maintain a sliding window such that the characters within the window are not repeated. Initially, we use a binary integer $\textit{mask}$ of length $26$ to represent the characters within the window, where the $i$-th bit being $1$ indicates that character $i$ has appeared in the window, otherwise it indicates that character $i$ has not appeared in the window.
class Solution:
def countGoodSubstrings(self, s: str) -> int:
ans = mask = l = 0
for r, x in enumerate(map(lambda c: ord(c) - 97, s)):
while mask >> x & 1:
y = ord(s[l]) - 97
mask ^= 1 << y
l += 1
mask |= 1 << x
ans += int(r - l + 1 >= 3)
return ansContinue Topic
hash table
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Sliding window with running state updates
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward