merge
to store the result.i
and j
to traverse word1
and word2
respectively.word1
and word2
are non-empty:
a. If the current character of word1
is greater than word2
, append it to merge
and increment i
.
b. If the current character of word2
is greater than word1
, append it to merge
and increment j
.
c. If the current characters are equal, compare the substrings from the current indices to the end.
d. Append the character from the string with the lexicographically larger substring and increment the respective pointer.merge
.merge
string.