Check for Palindrome after every character replacement Query | Function to check if string is Palindrome or Not ; Takes two inputs for Q queries . For every query , it prints Yes if string becomes palindrome and No if not . ; Process all queries one by one ; query 1 : i1 = 3 , i2 = 0 , ch = ' e ' query 2 : i1 = 0 , i2 = 2 , ch = ' s ' replace character at index i1 & i2 with new ' ch ' ; check string is palindrome or not ; Driver Code
def isPalindrome ( string : list ) -> bool : NEW_LINE INDENT n = len ( string ) NEW_LINE for i in range ( n // 2 ) : NEW_LINE INDENT if string [ i ] != string [ n - 1 - i ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def Query ( string : list , Q : int ) -> None : NEW_LINE INDENT for i in range ( Q ) : NEW_LINE INDENT inp = list ( input ( ) . split ( ) ) NEW_LINE i1 = int ( inp [ 0 ] ) NEW_LINE i2 = int ( inp [ 1 ] ) NEW_LINE ch = inp [ 2 ] NEW_LINE string [ i1 ] = string [ i2 ] = ch NEW_LINE if isPalindrome ( string ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = list ( " geeks " ) NEW_LINE Q = 2 NEW_LINE Query ( string , Q ) NEW_LINE DEDENT