Substrings of Size Three with Distinct Characters - Problem
Find All Good Substrings
Imagine you're analyzing text patterns to find "good" character sequences. A string is considered good when all its characters are unique - no repeating letters allowed!
Your mission: Given a string
Key Points:
Example: In "xyzzaz", we have substrings "xyz" (good โ), "yzz" (bad โ), "zza" (bad โ), "zaz" (good โ). Answer: 2
Imagine you're analyzing text patterns to find "good" character sequences. A string is considered good when all its characters are unique - no repeating letters allowed!
Your mission: Given a string
s, count how many good substrings of exactly length 3 exist within it.Key Points:
- ๐ฏ We only care about substrings with exactly 3 characters
- โจ All 3 characters must be different (like "abc", "xyz", "cat")
- ๐ Count every occurrence, even if the same substring appears multiple times
- ๐ A substring is a contiguous sequence of characters
Example: In "xyzzaz", we have substrings "xyz" (good โ), "yzz" (bad โ), "zza" (bad โ), "zaz" (good โ). Answer: 2
Input & Output
example_1.py โ Simple case
$
Input:
s = "xyzzaz"
โบ
Output:
2
๐ก Note:
The good substrings are "xyz" (positions 0-2) and "zaz" (positions 3-5). "yzz" and "zza" contain repeated characters so they don't count.
example_2.py โ All good substrings
$
Input:
s = "abcd"
โบ
Output:
2
๐ก Note:
We have two possible 3-character substrings: "abc" (all different) and "bcd" (all different). Both are good, so the answer is 2.
example_3.py โ Edge case
$
Input:
s = "aab"
โบ
Output:
0
๐ก Note:
The only 3-character substring is "aab" which has repeated 'a' characters, so it's not good. The answer is 0.
Constraints
- 1 โค s.length โค 100
- s consists of lowercase English letters only
- Note: We only count substrings of exactly length 3
Visualization
Tap to expand
Understanding the Visualization
1
Position the Frame
Place your 3-item window frame at the start of the street
2
Inspect Items
Check if all 3 visible items are different - if yes, count it!
3
Slide Forward
Move the frame one position right and repeat the inspection
Key Takeaway
๐ฏ Key Insight: Instead of examining every item in each window from scratch, we only need to check if the 3 current items are all different - a simple constant-time comparison!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code