Given a string of lowercase letters in the range ascii[a-z], determine the index of a character that can be removed to make the string a palindrome.  There may be more than one solution, but any will do.  If the word is already a palindrome or there is no solution, return -1.  Otherwise, return the index of a character to remove.  

Example 

$s="bcbc"$  

Either remove 'b' at index $0$ or 'c' at index $3$.  

Function Description  

Complete the palindromeIndex function in the editor below.    

palindromeIndex has the following parameter(s):  

string s: a string to analyze  

Returns  

int: the index of the character to remove or $-1$  

Input Format

The first line contains an integer $q$, the number of queries. 

Each of the next $q$ lines contains a query string $s$.

Constraints

$1 \leq q \leq 20$  
$1\leq\text{length of}s\leq10^5+5$  
All characters are in the range ascii[a-z].

Sample Input
STDIN   Function
-----   --------
3       q = 3
aaab    s = 'aaab' (first query)
baa     s = 'baa'  (second query)
aaa     s = 'aaa'  (third query)

Sample Output
3
0
-1

Explanation

Query 1: "aaab" 

Removing 'b' at index $3$ results in a palindrome, so return $3$.     

Query 2: "baa" 

Removing 'b' at index $0$ results in a palindrome, so return $0$.

Query 3: "aaa" 

This string is already a palindrome, so return $-1$.  Removing any one of the characters would result in a palindrome, but this test comes first.

Note: The custom checker logic for this challenge is available here.

Source: hackerrank | Difficulty: EASY
Tags: String algorithms


Solutions:
import sys
T = int(input())

def check(s, idx):
	s = s[0:idx] + s[idx + 1:len(s)]
	(i, j) = (0, len(s) - 1)
	while i < j:
		if s[i] != s[j]:
			return False
		(i, j) = (i + 1, j - 1)
	return True
for _ in range(T):
	s = sys.stdin.readline().strip()
	n = len(s)
	(i, j) = (0, n - 1)
	while i < j:
		if s[i] != s[j]:
			break
		(i, j) = (i + 1, j - 1)
	if i >= j:
		print(-1)
	elif check(s, i):
		print(i)
	else:
		print(j)


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	s = sys.stdin.readline().strip()
	if s == s[::-1]:
		print(-1)
	else:
		for i in range(len(s) // 2):
			if s[i] != s[-i - 1]:
				x = s[:i] + s[i + 1:]
				print(i if x == x[::-1] else len(s) - i - 1)
				break
		else:
			print(0)


def isPalindrome(s):
	n = len(s)
	(i, j) = (0, n - 1)
	while i < j:
		if s[i] != s[j]:
			return False
		(i, j) = (i + 1, j - 1)
	return True
t = int(input())
while t > 0:
	s = input().strip()
	n = len(s)
	if isPalindrome(s):
		print(-1)
	(i, j) = (0, n - 1)
	while i < j:
		if s[i] != s[j]:
			if isPalindrome(s[:i] + s[i + 1:]):
				print(i)
				j = i
				continue
			elif isPalindrome(s[:j] + s[j + 1:]):
				print(j)
				i = j
				continue
		(i, j) = (i + 1, j - 1)
	t -= 1


for _ in range(int(input())):
	string = input().strip()
	length = len(string)
	if length < 3:
		print('0')
		continue
	ind = 0
	flag = 1
	while ind < length / 2:
		if string[ind] == string[-1 - ind]:
			ind += 1
		else:
			flag = 0
			break
	if flag == 1:
		print('-1')
		continue
	flag = 1
	spl = ind
	while ind < length / 2:
		if string[ind] == string[-2 - ind]:
			ind += 1
		else:
			flag = 0
			break
	if flag == 1:
		print(length - 1 - spl)
	else:
		print(spl)


t = int(input())
for case_t in range(t):
	s = input().strip()
	n = len(s)
	mid = n // 2
	flag = True
	for i in range(mid):
		if s[i] != s[n - i - 1]:
			if s[i] == s[n - i - 2] and s[i + 1] == s[n - i - 3]:
				print(n - i - 1)
			else:
				print(i)
			flag = False
			break
	if flag:
		print(-1)


def palindrome_index(line):
	length = len(line)
	half = int(length / 2)
	for i in range(half):
		right = i
		left = length - 1 - i
		if line[right] != line[left]:
			if line[right + 1:half + 1].startswith(line[left:half:-1]):
				return right
			else:
				return left
	else:
		return -1
t = int(input())
for _ in range(t):
	print(palindrome_index(input().strip()))


def find_index(s):
	li = 0
	ri = len(s) - 1
	found = None
	while li < len(s) / 2:
		if s[li] == s[ri]:
			li += 1
			ri -= 1
			continue
		if found is None:
			found = (li, ri)
			li += 1
			continue
		else:
			return found[1]
	if found is None:
		return -1
	else:
		return found[0]
T = int(input())
for i in range(T):
	print(find_index(input().strip()))


def pal(stri):
	le = len(stri)
	for j in range(le // 2):
		if stri[j] != stri[le - j - 1]:
			return False
	return True
tests = int(input())
for test in range(tests):
	s = input().strip()
	le = len(s)
	ind = None
	for i in range(le // 2):
		if s[i] != s[le - i - 1]:
			newli = s[:i] + s[i + 1:]
			if pal(newli):
				ind = i
				break
			else:
				ind = le - i - 1
				break
	if ind == None:
		ind = -1
	print(ind)


T = int(input())
for t in range(T):
	chars = input().strip()
	l = len(chars)
	half = l // 2
	first_half = chars[:half]
	middle_char = chars[half:-half]
	second_half = chars[-half:]
	second_half = second_half[::-1]
	if first_half == second_half:
		print(-1)
		continue
	remove_at_index = -1
	for i in range(half):
		if first_half[i] == second_half[i]:
			continue
		if i < half - 1:
			if second_half[i] == first_half[i + 1] and (i == half - 2 or second_half[i + 1] == first_half[i + 2]):
				remove_at_index = i
			elif first_half[i] == second_half[i + 1] and (i == half - 2 or first_half[i + 1] == second_half[i + 2]):
				remove_at_index = l - i - 1
			else:
				continue
		else:
			remove_at_index = i if second_half[i] == middle_char else l - i - 1
		break
	print(remove_at_index)


def is_palindrome(string):
	size = len(string)
	for (i, ch) in enumerate(string):
		if ch != string[size - i - 1]:
			return False
	return True
for _ in range(int(input())):
	string = input().strip()
	rev_string = list(reversed(string))
	size = len(string)
	if is_palindrome(string):
		print(-1)
		continue
	for i in range(int(len(string) / 2)):
		if string[i] != rev_string[i]:
			done = False
			temp_i = i
			while not done:
				if string[temp_i] != rev_string[temp_i + 1]:
					print(i)
					done = True
				elif string[temp_i + 1] != rev_string[temp_i]:
					print(size - i - 1)
					done = True
				temp_i += 1
			break


def is_palindrome(s, idx=-1):
	if -1 != idx:
		s = s[:idx] + s[idx + 1:]
	(l, i) = (len(s), 0)
	while i < l // 2:
		if s[i] != s[l - 1 - i]:
			if -1 != idx:
				return -1
			left = is_palindrome(s, i)
			return left if -1 != left else is_palindrome(s, l - 1 - i)
		i += 1
	return idx
for _ in range(int(input())):
	print(is_palindrome(input().strip()))


def palindromeIndex(string):
	if string == string[::-1]:
		return -1
	else:
		for guess in range(len(string) // 2):
			if string[:guess + 1] != string[:-guess - 2:-1]:
				testString = string[:guess] + string[guess + 1:]
				if testString == testString[::-1]:
					return guess
				newGuess = len(testString) - guess
				testString = string[:newGuess] + string[newGuess + 1:]
				if testString == testString[::-1]:
					return newGuess
for _ in range(int(input())):
	print(palindromeIndex(input().rstrip()))


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	s = sys.stdin.readline().strip()
	if s == s[::-1]:
		print(-1)
	else:
		for i in range(len(s) // 2):
			if s[i] != s[-i - 1]:
				x = s[:i] + s[i + 1:]
				print(i if x == x[::-1] else len(s) - i - 1)
				break
		else:
			print(0)


from sys import stdin

def is_palindrome(line):
	for i in range(len(line) // 2):
		if line[i] != line[len(line) - i - 1]:
			return False
	return True

def palindrome_index(line):
	if is_palindrome(line):
		return -1
	for i in range(len(line) // 2):
		j = len(line) - i - 1
		if line[i] == line[j]:
			continue
		if is_palindrome(line[:i] + line[i + 1:]):
			return i
		return j
	return -1
T = int(stdin.readline())
for t in range(T):
	line = stdin.readline().strip()
	print(palindrome_index(line))


def palindromeIndex(s):
	ret = -1
	i = 0
	j = len(s) - 1
	while i < j:
		if s[i] == s[j]:
			i += 1
			j -= 1
		elif ret == -1:
			ret = i
			alt = j
			i += 1
		else:
			return alt
	return ret
T = int(input().strip())
for tt in range(T):
	s = input().strip()
	print(palindromeIndex(s))


def checkPalindrome(str, direction=1):
	i = 0
	j = len(str) - 1
	usedSkip = -1
	while j > i:
		if str[i] == str[j]:
			i += 1
			j -= 1
		elif usedSkip != -1:
			return (False, -1)
		elif direction == 1:
			usedSkip = i
			i += 1
		else:
			usedSkip = j
			j -= 1
	return (True, usedSkip)
n = int(input())
for i in range(n):
	str = input().strip()
	(leftSuccess, lefti) = checkPalindrome(str, 1)
	(rightSuccess, righti) = checkPalindrome(str, -1)
	if lefti != -1:
		print(lefti)
	elif righti != -1:
		print(righti)
	elif leftSuccess:
		print(-1)
	else:
		raise Exception('!')


T = int(input())
for i in range(T):
	s = input().rstrip()
	if s == s[::-1]:
		print(-1)
	else:
		for i in range(len(s) // 2):
			if s[i] != s[-i - 1]:
				x = s[:i] + s[i + 1:]
				print(i if x == x[::-1] else len(s) - i - 1)
				break
		else:
			print(0)


from sys import stdin
from collections import deque
T = int(stdin.readline().strip())
for _ in range(T):
	cur = [c for c in stdin.readline().strip()]
	idx = -1
	for (i, (a, b)) in enumerate(zip(cur, cur[::-1])):
		if a != b:
			idx = i
			break
	if idx == -1:
		print(-1)
		continue
	lp = cur[:idx] + cur[idx + 1:]
	if lp == lp[::-1]:
		print(idx)
		continue
	oposite_idx = len(cur) - idx - 1
	rp = cur[:oposite_idx] + cur[oposite_idx + 1:]
	if rp == rp[::-1]:
		print(oposite_idx)
		continue