question
Devise an algorithm to find the number of 'pairs' in an input string. A 'pair' for
this question can be defined as a situation in which two instances of the same
character are separated by a single other character.
ex. "ZaZ" has one pair made of Z's
ex 2. "ZaZa" has 2 pairs
ex. "ZaZ" has one pair made of Z's
ex 2. "ZaZa" has 2 pairs
Solution is O(n).
An O(n) solution involves iterating through the string and comparing the current
character with another one character away. The only trick in this question is to pay
attention to details: "separated by a single other character". This means we will
also need to compare the first or third character to the middle character and ensure
they are different. Also be sure to iterate only through 'length - 2' to avoid out
of bounds exceptions.