Build Palindrome
Description
Given a string, find the shortest possible string which can be achieved by adding characters to the end of initial string to make it a palindrome.
Note: A palindrome is a string that reads the same left-to-right and right-to-left.
Example
For st = "abcdc"
, the output should be
buildPalindrome(st) = "abcdcba"
.
Input/Output
-
[execution time limit] 4 seconds (js)
-
[input] string st
A string consisting of lowercase latin letters.
Guaranteed constraints:
3 ≤ st.length ≤ 10
.
- [output] string
[JavaScript (ES6)] Syntax Tips
1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}
Solution
1
2
3
4
5
6
7
8
9
10
11
function buildPalindrome(st) {
var i = 0;
var aux;
while(st != st.split('').reverse().join('')){
aux = st.split('')
aux.splice(st.length-i, 0 ,st[i])
st = aux.join('');
i++;
}
return st;
}