Problem · String
Encode or Decode Message
Learn this problemProblem statement
Encodes or decodes a message using a shared numeric key.
Input
- Operation:
- 1 = Encode
- 2 = Decode
- Message (string)
- Key (positive integer)
Encoding (Operation 1)
Duplicates each character based on the corresponding digit in the key.
Example:
Message: Open, Key: 123 → Output: Oppeen
Decoding (Operation 2)
Compresses repeated characters based on digits in the key.
Example:
Message: Oppeen, Key: 123 → Output: Open
Rules
Function
encodeOrDecode(operation: int, message: String, key: int) → StringExamples
Example 1
operation = 1message = "Open"key = 123return = "Oppeen"~~
Example 2
operation = 2message = "Oppeen"key = 123return = "Open"~~~