236. Compare version numbers

Given two version numbers, version1 and version2, compare them.

A version number is a string consisting of dot . separated integers. Each segment (called a revision) is a non-negative integer, and leading zeroes should be ignored during comparison.

You must compare the version numbers revision by revision from left to right:

  • If version1 < version2, return -1.
  • If version1 > version2, return 1.
  • Otherwise, return 0.

If one version number has fewer revisions, consider the missing revisions as 0.

Example 1:

Input: version1 = "1.2", version2 = "1.10"

Output: -1

Explanation: 2 < 10, so version1 < version2

Example 2:

Input: version1 = "1.01", version2 = "1.001"

Output: 0

Explanation: Both represent 1.1

Now Your Turn!

Pick the correct output for the given input

Input: version1 = "1.0", version2 = "1.0.0.0"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= version1.length, version2.length <= 500
  • version1 and version2 only contain digits and '.'
  • All given revisions will fit in a 32-bit integer
  • No trailing or leading dots

Fun Facts

0
class Solution {
public:
int compareVersion(string version1, string version2) {
// Your code goes here
}
};
Test Case

Input:

Version1
Version2