FunctionRacerProblemsRace

Largest Number From Two Rows

HardStacks and queuesPython, JavaScript, TypeScript, Java, C++

Two rows of digit tiles are given as the strings first and second. Take exactly k tiles in total, any number from each row, and lay them in a line. Tiles from the same row keep their order from that row, but tiles from the two rows can be interleaved in any way.

Return the largest number you can lay, as a string of exactly k digits (leading zeros are kept).

Example 1

first "3462"
second "958"
k 4
returns "9862"

Take 9 and 8 from second and 6 and 2 from first, in that order.

Example 2

first "58"
second "507"
k 5
returns "58507"

Both rows start with 5. Taking the 5 of first first lets its 8 come out before the 0 of second.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems