This code finds the index in the string and removes the one which will make the string a palindrome. If the string is already a palindrome, it should return -1.
This code works correct, but I am looking for a better implementation of it. Here is the problem description.
def palindrome(string): return string == string[::-1] def palindrome_index(string, start, end): while start < end: if string[start] == string[end]: start, end = start+1, end-1 continue if palindrome(string[start:end]): return end return start return -1 N = input() strings = [str(raw_input()) for _ in range(N)] for string in strings: print palindrome_index(string, 0, len(string)-1)
Can this be written shorter? The size of input strings can be as large as 105, hence a recursive approach won't work.