TopcoderArchiveVisit Topcoder
SRM26 Mar 2015
SRM 656

CorruptedMessage

Problem statement, definition, constraints, and public examples.

Problem Statement

Your friend just sent you a message. The message consisted of one or more copies of the same lowercase letter. For example, "aaaaa" and "xxxxxxxxx" are valid messages. Unfortunately, on its way to you the message became corrupted: exactly k letters of the original message were changed to some other letters. The message you received is s.

Given the String s and the int k, reconstruct the original message. More precisely, return a String that could have been the original message. It is guaranteed that at least one such String will always exist. If there are multiple possible answers, you may return any of them.

Definition

Class:
CorruptedMessage
Method:
reconstructMessage
Parameters:
String, int
Returns:
String
Method signature:
String reconstructMessage(String s, int k)
(be sure your method is public)

Constraints

  • The number of characters in s will be between 1 and 50, inclusive.
  • Each character in s will be a lowercase letter ('a'-'z').
  • k will be between 0 and the length of s, inclusive.
  • At least one possible original message will be consistent with s and k.

Examples

  1. "hello"
    3
    Returns: "lllll"
    The three corrupted characters have 0-based indices 0, 1, and 4.
  2. "abc"
    3
    Returns: "ddd"
    The original message can't be "aaa", "bbb", or "ccc", since we need to change exactly 3 characters. Some other possible answers include "qqq", "xxx", or "ppp".
  3. "wwwwwwwwwwwwwwwwww"
    0
    Returns: "wwwwwwwwwwwwwwwwww"
    No characters were corrupted.
  4. "ababba"
    3
    Returns: "aaaaaa"
    "bbbbbb" will also be accepted.
  5. "zoztxtoxytyt"
    10
    Returns: "oooooooooooo"
  6. "jlmnmiunaxzywed"
    13
    Returns: "mmmmmmmmmmmmmmm"
Back to all problems