human
Check if the characters in a string form a Palindrome in O ( 1 ) extra space | Utility function to get the position of first character in the string.
Get the position of first character in the string.
Utility function to get the position of last character in the string.
Get the position of last character in the string.
Function to check if the characters in the given string forms a Palindrome in O ( 1 ) extra space.
break , when all letters are checked.
if mismatch found , break the loop.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to check if the characters in a string form a Palindrome in O ( 1 ) extra space and the other steps required.
 def firstPos ( str , start , end ) :
	firstChar = - 1
	for i in range ( start , end + 1 ) :
		if ( str [ i ] >= ' a ' and str [ i ] <= ' z ' ) :
			firstChar = i
			break
	return firstChar
def lastPos ( str , start , end ) :
	lastChar = - 1
	for i in range ( start , end - 1 , - 1 ) :
		if ( str [ i ] >= ' a ' and str [ i ] <= ' z ' ) :
			lastChar = i
			break
	return lastChar
def isPalindrome ( str ) :
	firstChar = 0
	lastChar = len ( str ) - 1
	ch = True
	for i in range ( len ( str ) ) :
		firstChar = firstPos ( str , firstChar , lastChar ) ;
		lastChar = lastPos ( str , lastChar , firstChar ) ;
		if ( lastChar < 0 or firstChar < 0 ) :
			break
		if ( str [ firstChar ] == str [ lastChar ] ) :
			firstChar += 1
			lastChar -= 1
			continue
		ch = False
		break
	return ( ch )
if __name__ == " _ _ main _ _ " :
	str = " m 	 a ▁ 343 ▁ la ▁ y ▁ a ▁ l ▁ am "
	if ( isPalindrome ( str ) ) :
		print ( " YES " )
	else :
		print ( " NO " )