420. Restore Astronaut Transmission
Restore Astronaut Transmission

Astronauts in outer space send compressed transmissions to mission control. Each lowercase letter represents one communication symbol.

A repeated part is written as count[section]. It represents the contents of section repeated exactly count times. A section may contain other repeated sections.

Restore and return the complete astronaut transmission.

Class

AstronautTransmissionRestorer

Method

restoreTransmission

String restoreTransmission(String compressedTransmission)

Parameters

  • compressedTransmission: The compressed transmission sent by the astronauts.

Returns

Return the complete transmission after expanding every repeated section.

Transmission Rules

  • Every lowercase letter represents one communication symbol.
  • Digits appear only as part of a positive repetition count immediately followed by a bracketed section.
  • A repetition count may contain one or more digits and has a value between 1 and 300.
  • Every opening bracket '[' appears immediately after a repetition count.
  • Every bracketed section contains at least one lowercase letter or repetition instruction.
  • Bracketed sections may be nested.
  • The transmission is valid and all brackets are correctly matched.

Constraints

  • 1 ≤ compressedTransmission.length() ≤ 30
  • compressedTransmission contains only lowercase English letters, digits, '[', and ']'.
  • Every repetition count is between 1 and 300.
  • A repetition count does not contain leading zeros.
  • The restored transmission contains at most 100,000 characters.

Examples

Example 1

restoreTransmission(compressedTransmission = "3[uv]2[x]")

Output: "uvuvuvxx"

The section "uv" is repeated three times, followed by 'x' repeated twice.

Example 2

restoreTransmission(compressedTransmission = "2[m2[np]]")

Output: "mnpnpmnpnp"

The inner section produces "npnp". After adding 'm', the section "mnpnp" is repeated twice.

Example 3

restoreTransmission(compressedTransmission = "a2[b3[c]]d")

Output: "abcccbcccd"

The inner instruction produces "ccc". Therefore, "bccc" is repeated twice between 'a' and 'd'.

Example 4

restoreTransmission(compressedTransmission = "signal")

Output: "signal"

The transmission has no repeated sections, so it remains unchanged.



Please use Laptop/Desktop or any other large screen to add/edit code.