Find character at Kth index by appending S1 ( M ) times and S2 ( M + 1 ) times | Python program to solve the above approach ; initializing first and second variable as to store how many string ' s ' and string ' t ' will be appended ; storing tmp length ; if length of string tmp is greater than k , then we have reached our destination string now we can return character at index k ; appending s to tmp , f times ; appending t to tmp , s times ; extracting output character ; Driver code
def KthCharacter ( s , t , k ) : NEW_LINE INDENT f = 1 NEW_LINE ss = 2 NEW_LINE tmp = " " NEW_LINE lenn = len ( tmp ) NEW_LINE while ( lenn < k ) : NEW_LINE INDENT tf = f NEW_LINE ts = ss NEW_LINE while ( tf != 0 ) : NEW_LINE INDENT tf -= 1 NEW_LINE tmp += s NEW_LINE DEDENT while ( ts != 0 ) : NEW_LINE INDENT ts -= 1 NEW_LINE tmp += t NEW_LINE DEDENT f += 2 NEW_LINE ss += 2 NEW_LINE lenn = len ( tmp ) NEW_LINE DEDENT output = tmp [ k - 1 ] NEW_LINE return output NEW_LINE DEDENT S1 = " a " NEW_LINE S2 = " bc " NEW_LINE k = 4 NEW_LINE ans = KthCharacter ( S1 , S2 , k ) NEW_LINE print ( ans ) NEW_LINE