/165. Compare Version Numbers

165. Compare Version Numbers

Medium
Two Pointers46.2% acceptance
Given two strings, ver_str_a and ver_str_b, each representing a version number as a sequence of integer revisions separated by periods '.', compare the two version numbers. Each revision is an integer (leading zeros ignored). Compare revisions from left to right. If one version has fewer revisions, treat missing revisions as 0. Return -1 if ver_str_a < ver_str_b, 1 if ver_str_a > ver_str_b, and 0 if they are equal.

Example 1

Input: ver_str_a = 2.0.1, ver_str_b = 2.0.0

Output: 1

Explanation: Third revision: 1 > 0, so ver_str_a > ver_str_b.

Example 2

Input: ver_str_a = 3.4, ver_str_b = 3.4.0.0

Output: 0

Explanation: Missing revisions are treated as 0, so both are equal.

Example 3

Input: ver_str_a = 0.9.9, ver_str_b = 1.0.0

Output: -1

Explanation: First revision: 0 < 1, so ver_str_a < ver_str_b.

Constraints

  • 1 <= len(ver_str_a) <= 500
  • 1 <= len(ver_str_b) <= 500
  • ver_str_a and ver_str_b contain only digits and .
  • ver_str_a and ver_str_b are valid version numbers
  • Each revision value fits in a 32-bit integer
Python (current runtime)

Case 1

Input: ver_str_a = '10.0.0', ver_str_b = '10.0'

Expected: 0

Case 2

Input: ver_str_a = '1.2.3', ver_str_b = '1.2.4'

Expected: -1

Case 3

Input: ver_str_a = '1.2.3.4', ver_str_b = '1.2.3.4'

Expected: 0

Case 4

Input: ver_str_a = '1.2.3.4', ver_str_b = '1.2.3.3'

Expected: 1

Case 5

Input: ver_str_a = '1.0.0.0.0', ver_str_b = '1'

Expected: 0