TopcoderArchive
Archive
Algorithm problem

EncryptedMessage

Problem statement, definition, constraints, and public examples.

Problem Statement

Wireless devices are extremely vulnerable to unauthorized access. The information sent between wireless devices can be easily intercepted. To protect some private information the following encoding method may be used. For each chunk of the conversation, a secret String key is chosen. The key will have the same length as the chunk of the conversation, represented by message.
To find the ith character of the encoded message, we take the ith characters of key and message (lets call them ki and mi respectively). First we determine whether the resulting character will be upper- or lower- case. It will be lower-case only if exactly one of ki and mi is lower-case, and upper-case otherwise. Then we calculate the 0-based index of the character in the alphabet, which is equal to the sum of the 0-based indices of ki and mi modulo 26. For example, if key = "Key" and message = "ABc", the encoded message will be "KfA".

Given two Strings, key and message, return the encoded message.

Definition

Class:
EncryptedMessage
Method:
createMessage
Parameters:
String, String
Returns:
String
Method signature:
String createMessage(String key, String message)
(be sure your method is public)

Constraints

  • key and message will be the same length.
  • key and message will contain between 1 and 50 characters, inclusive.
  • key and message will contain only letters ('a'-'z', 'A'-'Z').

Examples

  1. "Key"
    "ABc"
    Returns: "KfA"
    'A' has index 0, and k0 and m0 are both uppercase, hence the first character of the encoded message is an uppercase 'K'. k1 is lowercase, while m1 is uppercase, making the second encoded character lowercase. Since 'B' has index 1, and 'e' has index 4, the resulting character has index 5, which is 'f'. The last character will be uppercase, and has an index of (24+2)%26 = 0, making it an 'A'.
  2. "ABcd"
    "dcBA"
    Returns: "dddd"
  3. "ZZZZZZZ"
    "aaaaaaa"
    Returns: "zzzzzzz"
  4. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
    "TheQuickBrownFoxJumpsOverTheLazyDog"
    Returns: "TheQuickBrownFoxJumpsOverTheLazyDog"
    An 'A' in the key leaves the corresponding character in message unchanged.
  5. "qfHEFqKrcXICJAruXuqpdmVINqkZYl"
    "YKtWshAgTuFhXeJkBxsRhwfdMmfTek"
    Returns: "opaAxXKXvrNjGeaEYRIgKIalZCPScV"
Back to all problems