human
Write me a code to number of positions where a letter can be inserted such that a string becomes palindrome and the other steps required given the following:
Number of positions where a letter can be inserted such that a string becomes palindrome | Function to check if the string is palindrome.
to know the length of string.
if the given string is a palindrome ( Case - I ).
Sub - case - III ).
if ( n % 2 == 0 ) : if the length is even.
count = 2 * count + 1 sub - case - I.
count = 2 * count + 2 sub - case - II.
insertion point.
Case - I.
Case - II.
Driver code"
Solution in Python:
assistant
#Here is code to number of positions where a letter can be inserted such that a string becomes palindrome and the other steps required.
 def isPalindrome ( s , i , j ) :
	p = j
	for k in range ( i , p + 1 ) :
		if ( s [ k ] != s [ p ] ) :
			return False
		p -= 1
	return True
def countWays ( s ) :
	n = len ( s )
	count = 0
	if ( isPalindrome ( s , 0 , n - 1 ) ) :
		for i in range ( n // 2 , n ) :
			if ( s [ i ] == s [ i + 1 ] ) :
				count += 1
			else :
				break
			count += 1
		else :
	else :
		for i in range ( n // 2 ) :
			if ( s [ i ] != s [ n - 1 - i ] ) :
				j = n - 1 - i
				if ( isPalindrome ( s , i , n - 2 - i ) ) :
					for k in range ( i - 1 , - 1 , - 1 ) :
						if ( s [ k ] != s [ j ] ) :
							break
						count += 1
					count += 1
				if ( isPalindrome ( s , i + 1 , n - 1 - i ) ) :
					for k in range ( n - i , n ) :
						if ( s [ k ] != s [ i ] ) :
							break
						count += 1
					count += 1
				break
	return count
if __name__ == " _ _ main _ _ " :
	s = " abca "
	print ( countWays ( s ) )