Company: NAB_28nov
Difficulty: medium
Longest Semi-Alternating Substring Problem Description You are given a string S of length N containing only characters 'a' and 'b'. A substring (contiguous fragment) of S is called a semi-alternating substring if it does not contain three identical consecutive characters. In other words, it does not contain either 'aaa' or 'bbb' as substrings. Note that the whole string S is its own substring. Write a function: class Solution { public int solution(String S); } which, given a string S , returns the length of the longest semi-alternating substring of S . Examples Example 1: Given S = "baaabbabbb" , your function should return 7. The longest semi-alternating substring is "aabbabb" . Example 2: Given S = "babba" , your function should return 5, since the whole string S is semi-alternating. Example 3: Given S = "abaaaa" , your function should return 4, because the first four letters of S ( "abaa" ) create a semi-alternating substring. Constraints Write an efficient algorithm for the followi