BackmediumStringsAtlassian

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'.

Example 1
Input
ACBACB
Output
5

Explanation: Step-by-step: with input ACBACB, we start with 'A', then 'C', then 'B', then 'A', then 'C', then 'B', giving output 5

Example 2
Input
ABCBA
Output
4

Explanation: Step-by-step: with input ABCBA, we start with 'A', then 'B', then 'C', then 'B', then 'A', giving output 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 Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Alternating Signal Sequences — Problem Statement & Solution Guide

StringsMediumMixed
TimeO(n)
|
SpaceO(1)

Problem Description

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

ACBACB

Output

5

Explanation: Step-by-step: with input ACBACB, we start with 'A', then 'C', then 'B', then 'A', then 'C', then 'B', giving output 5

Example 2

Input

ABCBA

Output

4

Explanation: Step-by-step: with input ABCBA, we start with 'A', then 'B', then 'C', then 'B', then 'A', giving output 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.

Optimal Approach & Strategy

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.

Brute Force Approach

The brute-force approach involves checking every possible substring of the input string to see if it meets the pattern requirements, resulting in a time complexity of O(n²). This approach is inefficient and not scalable for large inputs. It can be implemented using nested loops to generate all substrings and a separate function to check if each substring is valid.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function longestAlternatingSequence(str) {
  let maxLen = 0;
  let currLen = 0;
  let set = new Set();
  let startChar = null;

  for (let i = 0; i < str.length; i++) {
    if (set.size === 3) {
      if (str[i] === str[i - 1] || set.has(str[i])) {
        set.clear();
        currLen = 0;
        startChar = null;
      }
    }
    if (startChar === null) {
      startChar = str[i];
    }
    if (str[i] !== startChar) {
      set.add(str[i]);
      currLen++;
      maxLen = Math.max(maxLen, currLen);
    }
  }

  return maxLen;
}

Asked in Top Tech Interviews

Atlassian

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.