Compare Version Numbers

Given two version strings version1 and version2, made of revisions separated by dots, compare them revision by revision from left to right. Each revision is compared as an integer, so leading zeros are ignored, and a missing revision counts as 0. Return -1 if version1 < version2, 1 if version1 > version2, and 0 otherwise.

30:00

Compare Version Numbers
medium
Free written solution
Topics
Companies

Given two version strings version1 and version2, made of revisions separated by dots, compare them revision by revision from left to right. Each revision is compared as an integer, so leading zeros are ignored, and a missing revision counts as 0. Return -1 if version1 < version2, 1 if version1 > version2, and 0 otherwise.

Example 1:
Input: {"version1":"1.2","version2":"1.10"}
Output: -1
Constraints:
  • 1≤version1.length,version2.length≤5001 \leq \text{version1.length}, \text{version2.length} \leq 500

  • version1 and version2 only contain digits and '.'.

  • version1 and version2 are valid version numbers.

  • All the given revisions in version1 and version2 can be stored in a 32-bit integer.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"version1":"1.2","version2":"1.10"}

Put i at the start of version1 and j at the start of version2. Revision 1 is read first.

version1

1
.
2
▲
i

version2

1
.
1
0
▲
j

revisions compared

none yet

Variables
VariableValue
i0
j0
a-
b-
DepthFunction Call
Stack empty
0/12