BackmediumStrings Cred

Galactic Transmission Protocol Solution

Problem Statement

Given a string representing a sequence of transmission signals, find the length of the longest substring where the signals appear in an alternating sequence of 3 distinct signal types (denoted by 'A', 'B', and 'C'), with each signal type appearing only once in the sequence and the sequence starting and ending with the same signal type.

Example 1
Input
ABCBC
Output
5

Explanation: Step-by-step: with input ABCBC, we do A then B then C then B then C, but this sequence does not meet the condition of each signal type appearing only once. However, if we consider the sequence ABCBCA, it meets the condition of each signal type appearing only once and the sequence starting and ending with the same signal type. Therefore, the longest substring is ABCBCA, which has a length of 6. However, we need to find the longest substring that meets the condition, which is ABCBCA without the last A, giving a length of 5.

Example 2
Input
ABCB
Output
4

Explanation: Step-by-step: with input ABCB, we do A then B then C then B, but this sequence does not meet the condition of each signal type appearing only once. Therefore, the longest substring that meets the condition is ABC, which has a length of 3. However, we can extend this sequence to ABCB, which has a length of 4.

Constraints

  • The input string will only contain the characters 'A', 'B', and 'C'.
  • The length of the input string will not exceed 1000 characters.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free