Problem · String

Encode or Decode Message

EasyGoldman SachsOA

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.
  • Examples
    01 · Example 1
    operation = 1
    message = "Open"
    key = 123
    return = "Oppeen"
    ~~
    02 · Example 2
    operation = 2
    message = "Oppeen"
    key = 123
    return = "Open"
    ~~~
    More Goldman Sachs problems
    drafts saved locally
    public String encodeOrDecode(int operation, String message, int key) {
      // write your code here
    }
    
    operation1
    message"Open"
    key123
    expected"Oppeen"
    checking account