Longest Semi-Alternating Substring - Company-specific OAs / Microsoft OA

Given a string S 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' substrings. Note that the whole S is its own substring.

This is a companion discussion topic for the original entry at https://algo.monster/problems/longest_semi_alternating_substring/