A Palindrome Is Not a Palindrome!!
Time limit2sMemory limit512 MB
Given an uppercase string up to 500,000 characters, find the length of the longest substring that is not a palindrome, or -1 if none exists.
- Level
Easy3 of 10
- Topics
- String, Implementation, Greedy, Brute force
- Solved
- No attempts yet
Problem
A palindrome is a string that reads the same forward and backward. Examples of palindromes include POP and ABBA, and examples of non-palindromes include ABCA and PALINDROME.
Consider several words that share this meaning.
- 회문 (Korean)
- palindrome (English, French, Norwegian, Greek, Latin)
- 回文 (Japanese, Chinese)
- palindrom (German, Danish)
- palindromi (Finnish)
- palíndromo (Spanish, Portuguese)
- palindromo (Italian, Esperanto)
- палиндром (Russian)
- قلب مستو (Arabic)
Do you notice something strange? In no language is the word for palindrome itself a palindrome! The “beauty of symmetry” that many pursue is nothing but an illusion.
Given a string of uppercase letters, find the length of the longest substring that is not a palindrome. The characters of a substring must be consecutive. AB is a substring of ABCD, but AC is not.
Input
A string of length between 1 and 500,000 is given.
Output
Print the length of the longest substring that is not a palindrome. If no such substring exists, print -1.