-4

Possible Duplicate:
Write a function that returns the longest palindrome in a given string

Given String of 'n' length, I need the longest palindrome whose time and space complexity should be efficient.

Can anyone help me at least with the pseudo code?

Community
  • 1
  • 1
Sathish
  • 11
  • 4

1 Answers1

0

One approach would be to consider each character in the string as possible "center" of the palindrome, then expand left and right as long as the character at the left position is equal to the character on the right position (which is a requirement for the sub-string to be a palindrome, obviously have to consider the two sub-cases of sub-string being of odd and even length). Doing this for all positions 1..n in the source string will give you the longest palindrome from consecutive characters contained in the string.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335