Good morning! Here's our prompt for today.
We have a string str
like the following:
JAVASCRIPT
1const str = "bubble";
Find a way to convert it to a palindrome by inserting characters in front of it. Recall that a palindrome
is defined as "a word, phrase, or sequence that reads the same backward as forward".

What's the shortest palindrome that can be returned? For example, the following above string should return:
JAVASCRIPT
1shortestPalindrome("bubble")
2// "elbbubble"
Constraints
- Length of the given string <=
1000
- The string can contain any ASCII letters
- Expected time complexity :
O(n)
- Expected space complexity :
O(1)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
28
var assert = require('assert');
​
function shortestPalindrome(s) {
return s;
}
​
try {
assert.equal(shortestPalindrome('bubble'), 'elbbubble');
​
console.log(
'PASSED: ' +
"<code>shortestPalindrome('bubble')</code> should return <code>'elbbubble'</code>"
);
} catch (err) {
console.log(err);
}
​
try {
assert.equal(shortestPalindrome('dasndsadmx'), 'xmdasdnsadasndsadmx');
​
console.log(
'PASSED: ' +
"<code>shortestPalindrome('dasndsadmx')</code> should return <code>'xmdasdnsadasndsadmx'</code>"
);
} catch (err) {
console.log(err);
}
​
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
We'll now take you through what you need to know.
How do I use this guide?