Skip to the code
cf.
+25 XP

#609 Fewest Letters to Mirror a Name

medium

A playful username generator turns any name into a palindrome by adding letters at the end only.

Write mirrorName(name) that returns the shortest palindrome that starts with name, made by appending as few characters as possible to its end.

An empty name stays empty.

Example 1

Input: name = "race"
Output: "racecar"

e alone is the longest mirrored ending, so car (the reverse of rac) is appended.

Example 2

Input: name = "abcb"
Output: "abcba"

bcb is already mirrored, so only a is needed.

Constraints

  • 0 <= name.length <= 2000
  • name contains lowercase letters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).