easy

Substrings of Size Three with Distinct Characters

Count substrings of length three whose characters are all different.

1. Define the problem

Substrings of Size Three with Distinct Characters

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. Slide a fixed length window of length 3 across the string and check whether all three characters inside are different.

Constraints

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

Example

Inputs = "xyzzaz"
Output1

Explanation "xyz" is the only good substring of length three — the rest all repeat a character.

2. Visualize the solution

Check each window of size 3 for distinct characters

Check each window of size 3 for distinct characters
Statusgood

Window "xyz": x, y, z are all different — good substring. count = 1.

What happens in this step

window = "xyz"  [0, 2]

s[0]=x, s[1]=y, s[2]=z — all three different

Good substring — count = 1.
Step 1 of 4

Steps to visualize

  1. Place a window of length 3 on the first three characters.
  2. Check whether all three characters inside are different.
  3. If they are, count it as a good substring.
  4. Slide the window one step right and check again.
  5. Continue until the window reaches the end of the string.
3. Walk through the code

Walk through the code

Same walkthrough, now with the code. Press Next to move one step and watch which lines run.

Check each window of size 3 for distinct characters
Statusgood

Window "xyz": x, y, z are all different — good substring. count = 1.

What happens in this step

window = "xyz"  [0, 2]

s[0]=x, s[1]=y, s[2]=z — all three different

Good substring — count = 1.
Step 1 of 4
4. Solution

Solution

solution.tsTypeScript
function countGoodSubstrings(s) {
  if (s.length < 3) {
    return 0;
  }

  let count = 0;
  for (let left = 0; left + 2 < s.length; left++) {
    const a = s[left];
    const b = s[left + 1];
    const c = s[left + 2];
    if (a !== b && b !== c && a !== c) {
      count++;
    }
  }

  return count;
}
Time
O(n)
Space
O(1)
5. Test cases

Test cases

InputExpectedCovers
s = "xyzzaz"1example from the docstring
s = "ab"0string shorter than 3: no substrings of length 3 exist
s = "abc"1smallest valid input: exactly one good substring
s = "aaa"0smallest valid input: no distinct characters
s = "abcabc"4every substring of length 3 qualifies
s = "aabbcc"0no substrings qualify
s = "aababcabc"4larger, hand-verified case

Keep reading

TopicDescription
Sliding WindowSolve fixed-window problems: maximum sum of k elements, moving averages, and O(n) updates as the window slides.
Maximum Average Subarray IFind the contiguous subarray of a fixed length with the largest average.
Longest Substring Without Repeating CharactersFind the length of the longest run of characters in a string with no repeats.
Permutation in StringCheck whether one string contains a rearrangement of another string as a substring.
Minimum Size Subarray SumFind the shortest contiguous subarray whose sum is at least a target value.
Longest Repeating Character ReplacementFind the longest substring you can turn into one repeated letter with a limited number of swaps.