human
Palindrome by swapping only one character | Python3 program palindrome by swapping only one character.
counts the number of differences which prevents the string from being palindrome ..
keeps a record of the characters that prevents the string from being palindrome ..
loops from the start of a string till the midpoint of the string.
difference is encountered preventing the string from being palindrome.
3 rd differences encountered and its no longer possible to make is palindrome by one swap.
record the different character.
store the different characters.
its already palindrome.
only one difference is found.
if the middleChar matches either of the difference producing characters , return true.
two differences are found.
if the characters contained in the two sets are same , return true.
Driver Code"
How can the above be solved in Python?
 What is a code to palindrome by swapping only one character and the other steps required?
assistant
#Here is code to palindrome by swapping only one character and the other steps required.
 def isPalindromePossible ( input : str ) -> bool :
	length = len ( input )
	diffCount = 0
	i = 0
	diff = [ [ '0' ] * 2 ] * 2
	while i < length // 2 :
		if input [ i ] != input [ length - i - 1 ] :
			if diffCount == 2 :
				return False
			diff [ diffCount ] [ 0 ] = input [ i ]
			diff [ diffCount ] [ 1 ] = input [ length - i - 1 ]
			diffCount += 1
		i += 1
	if diffCount == 0 :
		return True
	elif diffCount == 1 :
		midChar = input [ i ]
		if length % 2 != 0 and ( diff [ 0 ] [ 0 ] == midChar or diff [ 0 ] [ 1 ] == midChar ) :
			return True
	elif diffCount == 2 :
		if ( diff [ 0 ] [ 0 ] == diff [ 1 ] [ 0 ] and diff [ 0 ] [ 1 ] == diff [ 1 ] [ 1 ] ) or ( diff [ 0 ] [ 0 ] == diff [ 1 ] [ 1 ] and diff [ 0 ] [ 1 ] == diff [ 1 ] [ 0 ] ) :
			return True
	return False
if __name__ == " _ _ main _ _ " :
	print ( isPalindromePossible ( " bbg " ) )
	print ( isPalindromePossible ( " bdababd " ) )
	print ( isPalindromePossible ( " gcagac " ) )