149. Which Release Is Newer

A package manager compares two release names. A release name is a list of revision numbers separated by dots, for example 2.10.0. Compare the revisions from left to right as numbers, so 10 is larger than 9 and leading zeros do not matter. A missing revision counts as 0, and so does an empty one (the text between two dots that follow each other).

Return 1 if v1 is the newer release, -1 if v2 is newer, and 0 if they mean the same release. A revision can have many digits, so compare them as text after removing the leading zeros: the longer text is larger, and equal lengths are compared letter by letter.

Example 1

Input:
v1 = "2.10.0", v2 = "2.9.9"
Output:
1
Explanation:

The first revisions are equal; then 10 is larger than 9, so v1 is newer.

Example 2

Input:
v1 = "1.04", v2 = "1.4.0"
Output:
0
Explanation:

04 is the same as 4, and the missing third revision counts as 0, so both mean the same release.

Constraints

1 ≤ v1.length, v2.length ≤ 105

v1 and v2 contain only digits and dots

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms

Expected complexity

Time
O(n + m)
Space
O(n + m)

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…