cswork / OA打开练习工作台

Goldman SachsOA #12

Encode or Decode Message

估计简单评测数据准备中,可先阅读题面

考点(AI 标注)字符串模拟/实现

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 Apply digits in key cyclically or until message ends. Remaining characters (if key ends first) are left unchanged. Return -1 for invalid input or mismatched patterns during decoding.

Example 1

Input:

operation = 1
message = "Open"
key = 123

Output:

"Oppeen"

Explanation: ~~

Example 2

Input:

operation = 2
message = "Oppeen"
key = 123

Output:

"Open"

Explanation: