AlgoDaily

Find Shortest Palindrome Possible

Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.

Description

We have a string str like the following:

const 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:

shortestPalindrome("bubble")
// "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)
```js
function shortestPalindrome(s) { 
  return s;
};
```

CONTINUE WITH ALGODAILY PREMIUM

Keep going with the complete tutorial.

Unlock the remaining explanations and exercises, alongside the full learning library.

Explore Premium →
Open interactive view →