Lexicographically Optimal Merging of Two Words

Company: Turing_24may

Difficulty: medium

Problem Statement

Lexicographically Optimal Merging of Two Words Problem Description You are given two strings, str1 and str2. Your goal is to construct a new string, result, by repeatedly performing one of the following operations until both str1 and str2 are empty: Take from str1: Append the first character of str1 to result and remove it from str1. For example, if str1 = "xyz" and result = "a", after this operation, str1 = "yz" and result = "ax". Take from str2: Append the first character of str2 to result and remove it from str2. For example, if str2 = "pq" and result = "ab", after this operation, str2 = "q" and result = "abp". Your objective is to construct the lexicographically largest string result. A string a is considered lexicographically larger than string b (of the same length) if, at the first position where they differ, the character in a is greater than the corresponding character in b. For example, "zxy" is lexicographically larger than "zxx" because y>x. Examples Example 1: Input: str1