1203. Release Tag Sort
A package registry lists releases written as MAJOR.MINOR.PATCH with an optional suffix -pre.release made of dot-separated identifiers. Sort versions in ascending order of precedence. Compare major, minor and patch as numbers. A version with no suffix ranks above the same numbers with a suffix.
Suffix identifiers are compared one by one: two numeric identifiers compare by value, a numeric identifier ranks below an alphanumeric one, and two alphanumeric ones compare by ASCII code. When all shared identifiers are equal, the shorter list ranks lower. The suffix starts at the first '-', and identifiers may contain further '-' characters.
Here 1 <= versions.length <= 1000, the versions are distinct and valid, every number is at most 10^9 without leading zeros, and identifiers use [0-9A-Za-z-]. Sorting takes O(n log n) comparisons and O(n) space.
Example 1
- Input:
- versions = ["1.0.0","1.0.0-beta","1.0.0-alpha","0.9.12"]
- Output:
- ["0.9.12","1.0.0-alpha","1.0.0-beta","1.0.0"]
- Explanation:
A release without a suffix ranks above the same version with one, so alpha and beta come first, then 1.0.0; 0.9.12 has the smallest minor number.
Example 2
- Input:
- versions = ["2.1.0","1.10.0","1.2.10","1.2.9","1.2.10-rc.1"]
- Output:
- ["1.2.9","1.2.10-rc.1","1.2.10","1.10.0","2.1.0"]
- Explanation:
Numbers compare by value, not text: 1.2.9 < 1.2.10-rc.1 < 1.2.10 < 1.10.0 < 2.1.0.
Example 3
- Input:
- versions = ["3.0.0-rc.1","3.0.0-rc.1.1","3.0.0-rc","3.0.0-rc.beta","3.0.0-rc.2","3.0.0-rc.10"]
- Output:
- ["3.0.0-rc","3.0.0-rc.1","3.0.0-rc.1.1","3.0.0-rc.2","3.0.0-rc.10","3.0.0-rc.beta"]
- Explanation:
Identifiers compare left to right: rc is a prefix of rc.1 so it is first, numeric 2 and 10 compare by value, and numeric 10 ranks below the alphanumeric beta.
Constraints
1 ≤ versions.length ≤ 1000
versions are distinct and valid
numbers are at most 109 without leading zeros
identifiers use [0-9A-Za-z-]
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 * L)
- Space
- O(n)