TopcoderArchiveVisit Topcoder
SRM17 Jan 2007
SRM 335

Palindromize

Problem statement, definition, constraints, and public examples.

Problem Statement

A palindrome is a string that reads the same from left to right as it does from right to left. Given a String s, return the shortest palindrome that can be produced by adding zero or more characters to the right end of s. There is always one unique answer.

Definition

Class:
Palindromize
Method:
minAdds
Parameters:
String
Returns:
String
Method signature:
String minAdds(String s)
(be sure your method is public)

Constraints

  • s will contain between 1 and 50 characters, inclusive.
  • Each character of s will be a lowercase letter ('a'-'z').

Examples

  1. "add"
    Returns: "adda"
    Add 'a' to make a palindrome. "adddda" is also a valid palindrome, but it is not the shortest possible one.
  2. "cigartragic"
    Returns: "cigartragic"
    This is already a palindrome, so the shortest solution is to add 0 letters.
  3. "redocpot"
    Returns: "redocpotopcoder"
  4. "acaba"
    Returns: "acabaca"
  5. "abczyxyz"
    Returns: "abczyxyzcba"
Back to all problems