Longest 2 character substring
Given a string find the longest substring that has only 2 characters.
Solution: This problem is extension of the problem where given a string we have to find the longest substring that has only one character. For finding the longest single character substring, we keep 2 variables one to keep track of longest substring so far and other to count the frequency of current longest substring candidate. Similarly to find the longest 2 character substring we keep 3 variables, two to keep track of longest substring so far and current candidate like the single character problem, the third variable is used to track the frequency of last character. Following c++ implementation is self explanatory:
https://gist.github.com/nirvana-attained/7433191
Analysis: Worst case time complexity is O(n) when n is the length of string. Space complexity is O(1).













