X171: countPairs

We'll say that a "pair" in a string is two instances of the same character separated by another character. So in "AxA", the A's make a pair. Pairs can overlap, so "AxAxA" contains 3 pairs--the first pair of A's, the second pair of A's, and also one pair of x's. Recursively compute (no loops) the number of pairs in the given string.

Examples:

countPairs("axa") -> 1
countPairs("axax") -> 2

Your Answer:

Feedback

Your feedback will appear here when you check your answer.