Substrings of Size Three with Distinct Characters

easy sliding window

Problem

Return the number of length-3 substrings of s with three distinct characters.

Inputs = "xyzzaz"
Output1
Only "xyz" qualifies.

def count_good_substrings(s):
    cnt = 0
    for i in range(len(s) - 2):
        a, b, c = s[i], s[i+1], s[i+2]
        if a != b and b != c and a != c:
            cnt += 1
    return cnt
function countGoodSubstrings(s) {
  let cnt = 0;
  for (let i = 0; i + 2 < s.length; i++) {
    if (s[i] !== s[i+1] && s[i+1] !== s[i+2] && s[i] !== s[i+2]) cnt++;
  }
  return cnt;
}
class Solution {
    public int countGoodSubstrings(String s) {
        int cnt = 0;
        for (int i = 0; i + 2 < s.length(); i++) {
            char a = s.charAt(i), b = s.charAt(i+1), c = s.charAt(i+2);
            if (a != b && b != c && a != c) cnt++;
        }
        return cnt;
    }
}
int countGoodSubstrings(string s) {
    int cnt = 0;
    for (int i = 0; i + 2 < (int)s.size(); i++) {
        if (s[i] != s[i+1] && s[i+1] != s[i+2] && s[i] != s[i+2]) cnt++;
    }
    return cnt;
}
Time: O(n) Space: O(1)