FunctionRacerProblemsRace

Equal Neighbours

BeginnerRecursionPython, JavaScript, TypeScript, Java, C++

Return how many positions i in s hold the same character as position i + 1. Every adjacent pair is counted on its own, so a run of three equal characters counts twice. A string with fewer than 2 characters gives 0.

For example, "aabbb" has the adjacent pairs aa, bb and bb, so the answer is 3.

Example 1

s "aabbb"
returns 3

Positions 0 and 1 hold a and a; positions 2 and 3, and 3 and 4, hold b and b. That is 3 pairs.

Example 2

s "abc"
returns 0

No two neighbouring characters are equal.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Recursion problems