Skip to content

214. Shortest Palindrome #577

Discussion options

You must be logged in to vote

We need to find the shortest palindrome by adding characters in front of a given string. We can approach this by identifying the longest prefix of the string that is already a palindrome. Once we have that, the remaining part can be reversed and added to the front to make the entire string a palindrome.

Approach:

  1. Identify the longest palindromic prefix: We want to find the longest prefix of the string s that is already a palindrome.
  2. Reverse the non-palindromic suffix: The part of the string that isn't part of the palindromic prefix is reversed and added to the beginning of the string.
  3. Concatenate the reversed suffix and the original string to form the shortest palindrome.

To do this eff…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Sep 20, 2024
Maintainer Author

@basharul-siddike
Comment options

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested hard Difficulty
2 participants