Kth character from the Nth string obtained by the given operations | Function to return Kth character from recursive string ; If N is 1 then return A ; Iterate a loop and generate the recursive string ; Update current string ; Change A to B and B to A ; Reverse the previous string ; Return the kth character ; Driver Code
def findKthChar ( n , k ) : NEW_LINE INDENT prev = " A " NEW_LINE cur = " " NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return ' A ' NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT cur = prev + " B " NEW_LINE temp1 = [ y for y in prev ] NEW_LINE for i in range ( len ( prev ) ) : NEW_LINE INDENT if ( temp1 [ i ] == ' A ' ) : NEW_LINE INDENT temp1 [ i ] = ' B ' NEW_LINE DEDENT else : NEW_LINE INDENT temp1 [ i ] = ' A ' NEW_LINE DEDENT DEDENT temp1 = temp1 [ : : - 1 ] NEW_LINE prev = " " . join ( temp1 ) NEW_LINE cur += prev NEW_LINE prev = cur NEW_LINE DEDENT return cur [ k - 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE K = 3 NEW_LINE print ( findKthChar ( N , K ) ) NEW_LINE DEDENT