Introduction
A palindromic closure of an input string is the shortest palindrome that can be constructed out of the input string where the final palindrome starts with the input string.
For this challenge, we will consider a two-way palindromic closure such that
- Left Palindromic Closure of an input string is the shortest palindrome possible that starts with the input string.
- Right Palindromic Closure of an input string is the shortest palindrome possible that ends with the input string.
- Two-way Palindromic Closure of an input string is the shorter of either of Left or Right Palindromic Closure of the input string.
Task
Your task is simple. Given a string (consisting only of printable ASCII, new lines and white spaces), output the two-way palindromic closure of that string. In case of tie, either of the left or right palindromic closures are valid output.
You may write a program or function, taking input via STDIN (or closest alternative), command-line argument or function argument, and either printing the result to STDOUT (or closest alternative) or returning it as a string.
You can assume that the input will never be an empty string.
Few examples:
<Input> -> <Output>
"abcdef" -> "abcdefedcba" (or "fedcbabcdef")
"abcba" -> "abcba"
"abcb" -> "abcba"
"cbca" -> "acbca"
Initial Idea credit goes to VisualMelon, final idea with help from Martin and Zgarb
The terms palindromic closure, left-pallindromic closure and right-palindromic closure were first used and defined by this paper.