Equal Neighbours
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
0 <= len(s) <= 60shas lowercase letters only
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Recursion problems
- Halving TotalBeginner
- Largest DigitBeginner
- Multiply It OutBeginner
- Nested SumBeginner
- Rising DigitsBeginner
- Ruler PatternBeginner
- Same Both WaysBeginner
- Suffix ChainBeginner