1352. Version Tag Order

Easy1.5 points estimatedCustom ComparatorStringSorting

A release page lists software versions such as 1.2.10, written as dot-separated numeric components. Sort the array versions in ascending order by comparing components numerically from left to right, so 1.2.9 comes before 1.2.10. When one version has fewer components, each missing component counts as 0. If two versions are numerically equal, like 1.0 and 1, the one with fewer components goes first, and if they are still tied, they keep their order from the input. Return the sorted array of the original strings. Aim for O(n log n) comparisons, each costing at most 6 steps.

Example 1

Input:
versions = ["1.2.10","1.2.9","1.10"]
Output:
["1.2.9","1.2.10","1.10"]
Explanation:

Components are compared as numbers, so 9 comes before 10, and 1.2.x comes before 1.10 because 2 is smaller than 10.

Example 2

Input:
versions = ["1.0","1","1.0.0","0.9"]
Output:
["0.9","1","1.0","1.0.0"]
Explanation:

0.9 is the smallest; 1, 1.0 and 1.0.0 are numerically equal, so the one with fewer components goes first.

Example 3

Input:
versions = ["2.0.1","2.0.1","2.0.1.0","2.0"]
Output:
["2.0","2.0.1","2.0.1","2.0.1.0"]
Explanation:

2.0 is smaller than 2.0.1, and 2.0.1.0 is numerically equal to 2.0.1, so the shorter copies come first in input order.

Constraints

1 ≤ n ≤ 200

Each version has 1 to 6 components, each an integer from 0 to 104 written without leading zeros

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n log n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…