Skip to the code
cf.
+10 XP

#128 Distinct Three-Letter Slices

easy

A short-code generator cuts a long random string s into candidate codes: every slice of exactly 3 neighboring characters. A slice is usable only if its three characters are all different.

Write countUsableCodes(s) that returns how many slices of length 3 have no repeated character. Slices at different positions count separately, even if they spell the same code.

Example 1

Input: s = "xyzzaz"
Output: 1

The slices are xyz, yzz, zza, zaz; only xyz has three different characters.

Example 2

Input: s = "aababcabc"
Output: 4

abc, bca, cab, abc are usable.

Constraints

  • 0 <= s.length <= 100000
  • s contains lowercase letters and digits.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).