Count Good Substrings

EasyStringSliding WindowHash TableMath

Description

A string is good if there are no repeated characters. Given a lowercase string s, return the number of good substrings of length exactly three. A substring is a contiguous sequence of characters in the string. Substrings that occur at different positions are counted separately.

Examples

Input:s = "xyzzaz"
Output:1
Explanation:

Length-3 substrings: 'xyz', 'yzz', 'zza', 'zaz'. Only 'xyz' has no repeats.

Input:s = "aababcabc"
Output:4
Explanation:

Good substrings: 'abc' (twice), 'bca', 'cab'.

Input:s = "abc"
Output:1
Explanation:

Single length-3 substring 'abc' has no repeats.

Input:s = "aaa"
Output:0
Explanation:

Only substring 'aaa' has repeated characters.

Constraints

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

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.