Lexicographically smallest string possible by merging two sorted strings | Function to find lexicographically smallest possible by merging two sorted strings ; Stores length of s1 ; Stores length of s2 ; Pointer to beginning of string1 i . e . , s1 ; Pointer to beginning of string2 i . e . , s2 ; Stores the final string ; Traverse the strings ; Append the smaller of the two current characters ; Append the remaining characters of any of the two strings ; Print the final string ; Driver Code ; Function Call
def mergeStrings ( s1 , s2 ) : NEW_LINE INDENT len1 = len ( s1 ) NEW_LINE len2 = len ( s2 ) NEW_LINE pntr1 = 0 NEW_LINE pntr2 = 0 NEW_LINE ans = " " NEW_LINE while ( pntr1 < len1 and pntr2 < len2 ) : NEW_LINE INDENT if ( s1 [ pntr1 ] < s2 [ pntr2 ] ) : NEW_LINE INDENT ans += s1 [ pntr1 ] NEW_LINE pntr1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += s2 [ pntr2 ] NEW_LINE pntr2 += 1 NEW_LINE DEDENT DEDENT if ( pntr1 < len1 ) : NEW_LINE INDENT ans += s1 [ pntr1 : pntr1 + len1 ] NEW_LINE DEDENT if ( pntr2 < len2 ) : NEW_LINE INDENT ans += s2 [ pntr2 : pntr2 + len2 ] NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S1 = " abdcdtx " NEW_LINE S2 = " achilp " NEW_LINE mergeStrings ( S1 , S2 ) NEW_LINE DEDENT