You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A palindrome is a string that is spelled the same forward and backwards. Given a string
3
+
<b>base</b> that may or may not be a palindrome, we can always force <b>base</b> to be a palindrome by adding
4
+
letters to it. For example, given the word "RACE", we could add the letters "CAR" to its back to get "RACECAR" (quotes for clarity only). However, we are not restricted to adding letters at the back. For example, we
5
+
could also add the letters "ECA" to the front to get "ECARACE". In fact, we can add letters anywhere in the word, so we
6
+
could also get "ERCACRE" by adding an 'E' at the beginning, a 'C' after the 'R', and another 'R' before the final 'E'.
7
+
Your task is to make <b>base</b> into a palindrome by adding as few letters
8
+
as possible and return the resulting string. When there is more than one palindrome of minimal length that can be made, return the lexicographically earliest (that is, the one that occurs first in alphabetical order).
9
+
</p>
10
+
11
+
</td></tr><tr><tdcolspan="2"><h3>Definition</h3></td></tr><tr><td>    </td><td><table><tr><td>Class:</td><td>ShortPalindromes</td></tr><tr><td>Method:</td><td>shortest</td></tr><tr><td>Parameters:</td><td>string</td></tr><tr><td>Returns:</td><td>string</td></tr><tr><td>Method signature:</td><td>string shortest(string base)</td></tr><tr><tdcolspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><tdcolspan="2"><h3>Limits</h3></td></tr><tr><td>    </td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>64</td></tr></table></td></tr><tr><tdcolspan="2"><h3>Constraints</h3></td></tr><tr><tdalign="center" valign="top">-</td><td><b>base</b> contains between 1 and 25 characters, inclusive.</td></tr><tr><tdalign="center" valign="top">-</td><td>Every character in <b>base</b> is an uppercase letter ('A'-'Z').</td></tr><tr><tdcolspan="2"><h3>Examples</h3></td></tr><tr><tdalign="center" nowrap="true">0)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>"RACE"</pre></td></tr></table></td></tr><tr><td><pre>Returns: "ECARACE"</pre></td></tr><tr><td><table><tr><tdcolspan="2">To make "RACE" into a palindrome, we must add at least three letters. However, there are
12
+
eight ways to do this by adding exactly three letters:
Of these alternatives, "ECARACE" is the lexicographically earliest.
18
+
</td></tr></table></td></tr></table></td></tr><tr><tdalign="center" nowrap="true">1)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>"TOPCODER"</pre></td></tr></table></td></tr><tr><td><pre>Returns: "REDTOCPCOTDER"</pre></td></tr><tr><td></td></tr></table></td></tr><tr><tdalign="center" nowrap="true">2)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>"Q"</pre></td></tr></table></td></tr><tr><td><pre>Returns: "Q"</pre></td></tr><tr><td></td></tr></table></td></tr><tr><tdalign="center" nowrap="true">3)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>"MADAMIMADAM"</pre></td></tr></table></td></tr><tr><td><pre>Returns: "MADAMIMADAM"</pre></td></tr><tr><td></td></tr></table></td></tr><tr><tdalign="center" nowrap="true">4)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>"ALRCAGOEUAOEURGCOEUOOIGFA"</pre></td></tr></table></td></tr><tr><td><pre>Returns: "AFLRCAGIOEOUAEOCEGRURGECOEAUOEOIGACRLFA"</pre></td></tr><tr><td></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>
0 commit comments