Company: NAB_28nov
Difficulty: medium
Longest Semi-Alternating Substring Problem Description You are provided with a binary-like string S of length N , where every character is either 'a' or 'b' . We define a contiguous fragment of S as semi-alternating when no character appears three times in a row anywhere inside that fragment — that is, neither the pattern "aaa" nor the pattern "bbb" can be found within it. Observe that the entire string S itself qualifies as one of its substrings. Your task is to implement the following function: class Solution { public int solution(String S); } Given the input string S , the function must compute and return the maximum possible length among all semi-alternating substrings of S . Examples Example 1: For the input S = "baaabbabbb" , the expected output is 7, because the longest qualifying substring is "aabbabb" . Example 2: For the input S = "babba" , the expected output is 5, since the complete string already satisfies the semi-alternating property. Example 3: For the input S = "