Problem · String
Encode or Decode Message
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
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
public String encodeOrDecode(int operation, String message, int key) {
// write your code here
}
operation1
message"Open"
key123
expected"Oppeen"
checking account