Skip to content
Onsite

Compare Version Numbers

MediumP2Coding

Version strings are made of non-negative integers called revisions, joined by dots, for example 1.2.10. Leading zeros in a revision are ignored (01 equals 1), and a missing revision counts as 0, so 1.0 equals 1.0.0.

Compare version1 and version2 revision by revision from left to right. Return -1 if version1 is smaller, 1 if it is larger, and 0 if they are equal.

Example 1

Input
version1 = "1.01", version2 = "1.001"
Output
0

Example 2

Input
version1 = "1.0", version2 = "1.0.0"
Output
0

Example 3

Input
version1 = "0.1", version2 = "1.1"
Output
-1

Constraints

  • 1 <= version1.length, version2.length <= 500
  • Versions contain only digits and '.'.
  • Every revision fits in a 32-bit integer.
  • No version starts or ends with '.' and there are no two consecutive dots.

Hints

Run checks the 3 visible tests, Submit all 12.