Largest Number From Two Rows
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
0 <= len(first) <= 120 <= len(second) <= 121 <= k <= len(first) + len(second)firstandsecondhold digits only
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Stacks and queues problems
- Second Larger Item to the RightHard
- Smallest Pick With a Letter QuotaHard
- Tiny Language With LetHard
- Value of a Logic FormulaHard
- Close Every BracketBeginner
- Crate PileBeginner
- Dead-End DrivewayBeginner
- Elevator TripsBeginner