BackmediumStack TCS

Parsing Galactic Transmission Sequences Solution

Problem Statement

Implement a function to validate galactic transmission sequences. A sequence is valid if every opening signal is matched with a corresponding closing signal of the same frequency, and the signal pairs are properly nested.

Example 1
Input
S = 'AABBBG'
Output
false

Explanation: Step-by-step: with input S = 'AABBBG', we push 'A' onto the stack. Then we encounter 'B', which is a valid pair with 'A' on the stack. We push 'B' onto the stack. Then we encounter 'B' again, which is a valid pair with 'B' on the stack. Then we encounter 'B' again, which is a valid pair with 'B' on the stack. Then we encounter 'G', which does not have a corresponding opening signal, so the sequence is invalid.

Example 2
Input
S = 'AAABBB'
Output
true

Explanation: Step-by-step: with input S = 'AAABBB', we push 'A' onto the stack. Then we encounter 'A', which is a valid pair with 'A' on the stack. We push 'A' onto the stack. Then we encounter 'B', which is a valid pair with 'A' on the stack. We push 'B' onto the stack. Then we encounter 'B' again, which is a valid pair with 'B' on the stack. Then we encounter 'B' again, which is a valid pair with 'B' on the stack.

Constraints

  • 1 <= sequence length <= 1000
  • 0 <= signal frequency <= 10000
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