mediumStringsPattern: Mixed
Alternating Signal Sequences Solution
Problem Statement
Given a string of signals consisting of 'A', 'B', and 'C', find the length of the longest substring where the signals appear in an alternating sequence of 3 distinct signal types, with each signal type appearing only once in the sequence and the sequence starting and ending with either 'A' or 'C'.
Examples
Example 1:
Input:ABCABC
Output:6
Explanation: The longest substring with alternating sequence of 3 distinct signal types is the entire string
Example 2:
Input:ABABAB
Output:0
Explanation: No substring has all 3 distinct signal types
Constraints
- The input string will only contain the characters 'A', 'B', and 'C'.
- The length of the input string will not exceed 1000 characters.
Time: O(n) Space: O(1)
The optimal approach uses a sliding window technique to efficiently scan the input string, keeping track of the maximum length of the valid alternating sequence found so far. This approach has a time complexity of O(n) and can be implemented using a single loop to iterate over the input string.
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
