FastPrepFastPrep
Problem Brief

Encode or Decode Message

OA

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.
  • 1Example 1

    Input
    operation = 1, message = "Open", key = 123
    Output
    "Oppeen"
    Explanation
    ~~

    2Example 2

    Input
    operation = 2, message = "Oppeen", key = 123
    Output
    "Open"
    Explanation
    ~~~
    public String encodeOrDecode(int operation, String message, int key) {
      // write your code here
    }
    
    Input

    operation

    1

    message

    "Open"

    key

    123

    Output

    "Oppeen"

    Sign in to submit your solution.