Substrings of Size Three with Distinct Characters - Problem

A string is good if there are no repeated characters.

Given a string s, return the number of good substrings of length three in s.

Note that if there are multiple occurrences of the same substring, every occurrence should be counted.

A substring is a contiguous sequence of characters in a string.

Input & Output

Example 1 — Basic Case
$ Input: s = "xyzzaz"
Output: 1
💡 Note: Only "xyz" at the beginning has all distinct characters. "yzz", "zza", and "zaz" all have repeated characters.
Example 2 — Multiple Good Substrings
$ Input: s = "aababcabc"
Output: 4
💡 Note: The good substrings are: "bab" (index 2), "abc" (index 3), "bca" (index 4), "cab" (index 5), "abc" (index 6). Total = 4 distinct character substrings.
Example 3 — No Good Substrings
$ Input: s = "aaa"
Output: 0
💡 Note: Only one substring "aaa" exists, but it has repeated characters, so no good substrings.

Constraints

  • 1 ≤ s.length ≤ 100
  • s consists of lowercase English letters.

Visualization

Tap to expand
Good Substrings of Size Three with Distinct CharactersInput: "aababcabc"aababcabc012345678"bab" - Good"abc" - Good"bca" - Good"cab" - GoodOutput: 4 good substrings
Understanding the Visualization
1
Input
String with characters to analyze
2
Process
Check each 3-character window for distinct characters
3
Output
Count of substrings with all unique characters
Key Takeaway
🎯 Key Insight: Check each 3-character window by comparing all pairs - no complex data structures needed
Asked in
Amazon 25 Facebook 18 Google 15 Microsoft 12
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen