Longest Palindromic Substring

View as PDF

Submit solution

Points: 10
Time limit: 2.0s
Memory limit: 256M

Author:
Problem types

A palindrome is a sequence of characters such that it is the same when read forwards and backwards. For instance, BOB, RACECAR are palindromes, but BRIAN is not.

You are to write a program that finds the longest palindrome in a given string of characters.

Input Specification

The first line of input will contain one positive integer N (1 \le N \le 1000), the number of characters in the string. The next line will contain a string of length N. The input string will contain only lower case alphabetical characters.

Output Specification

Your program should output a single integer, containing the length of the longest palindrome contained in the original string.

Sample Input 1

8
bcabcbac

Sample Output 1

5

Explanation of Sample Output 1

The longest palindromic substring is abcba.

Sample Input 2

7
racecar

Sample Output 2

7

Sample Input 3

7
jeffrey

Sample Output 3

2

Comments

There are no comments at the moment.