Editorial
Represent the four states by . The action of each gate is the following permutation.
H:X:Z:
Fix the left endpoint and move the right endpoint from to . Initially, the four resulting states are . When gate is added, apply its permutation to all four current results. If the results become again, add interval to the answer.
Repeat this for every . The time complexity is , and the additional space complexity is .
Solution written by GPT6