#40 - compare two versions

You are given two version numbers version1 and version2, represented as strings. Your task is to implement a function to compare the two versions.

  • If version1 is greater than version2, return 1.

  • If version1 is less than version2, return -1.

  • If version1 is equal to version2, return 0.

Each version number may contain multiple components separated by dots (.). Each component contains only digits and may not contain leading zeros, except for the zero itself.

Example 1

version1 = "1.0.1"
version2 = "1"
1

Example 2

version1 = "7.5.2.4"
version2 = "7.5.3"
-1

Example 3

version1 = "1.1.1"
version2 = "1.1.1"
0

Last updated