TopcoderArchiveVisit Topcoder
SRM20 Feb 2017
SRM 712

MakePalindrome

Problem statement, definition, constraints, and public examples.

Problem Statement

You have some cards. Each card contains a single lowercase letter. You are given these letters as the characters of the String card.

A palindrome is a string that reads the same forwards and backwards. Examples of palindromes: "eve", "abba", "aaaaaa", and "racecar".

Use the cards you have to spell some palindromes. In particular:
  • Each card must be used in exactly one of the palindromes.
  • The total number of palindromes must be as small as possible.
Return a String[] containing the palindromes you built. (Each element of the return value should be one of the palindromes.)

A solution always exists. If there are multiple optimal solutions, you may choose and output any one of them.

Definition

Class:
MakePalindrome
Method:
constructMinimal
Parameters:
String
Returns:
String[]
Method signature:
String[] constructMinimal(String card)
(be sure your method is public)

Constraints

  • card will contain between 1 and 1,000 characters, inclusive.
  • Each character in card will be a lowercase English letter ('a'-'z').

Examples

  1. "abbaa"
    Returns: {"ababa" }
    We can rearrange all letters into a single palindrome. There are two ways to do so: one is "ababa", the other is "baaab".
  2. "abc"
    Returns: {"a", "b", "c" }
    This time the only solution is to build three palindromes, each consisting of a single letter. Note that you may return the three strings in any order.
  3. "aaabbbccc"
    Returns: {"aba", "bcb", "cac" }
    There are other solutions like {"aaa", "bbb", "ccc"}
  4. "topcoder"
    Returns: {"oco", "d", "e", "p", "r", "t" }
  5. "z"
    Returns: {"z" }
Back to all problems