173. Trios of Different Letters

A puzzle magazine prints a strip of lowercase letters, s, and asks readers to find the rainbow trios. A rainbow trio is a block of exactly three consecutive letters in which no letter appears more than once, for example "xyz" but not "xyx".

Return how many positions of the strip start a rainbow trio. Blocks are counted by their position, so two identical-looking blocks at different positions both count. If the strip has fewer than three letters, the answer is 0.

Example 1

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

The blocks are xyz (all different), yzz (z twice) and zzy (z twice), so only one block counts.

Example 2

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

The blocks abc, bca, cab and abc each have three different letters, so all four count, one per starting position.

Constraints

1 ≤ s.length ≤ 105

s consists of lowercase English letters.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…