We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Compare Version Numbers

Number: 165

Difficulty: Medium

Paid? No

Companies: Nextdoor, Microsoft, Apple, Amazon, TikTok, Zoho, Bloomberg, Google, Oracle, Adobe


Problem Description

Given two version strings version1 and version2 (e.g., "1.2" vs. "1.10"), we need to compare them by splitting them into revisions (separated by '.') and comparing the integer value of each revision from left to right. If one version string has fewer revisions, the missing revisions are considered as 0. Return -1 if version1 is smaller, 1 if version1 is larger, and 0 if they are equal.


Key Insights

  • Split each version string into its revision parts using the '.' as a delimiter.
  • Convert each revision to an integer to eliminate the effect of leading zeros.
  • Iterate through both lists simultaneously by treating missing revisions as 0.
  • Compare the corresponding revisions; return as soon as a mismatch is found.
  • If all corresponding revisions are equal, the version strings are equivalent.

Space and Time Complexity

Time Complexity: O(max(m, n)), where m and n are the number of revisions in version1 and version2 respectively. Space Complexity: O(m + n) for storing the split revisions.


Solution

The solution involves the following steps:

  1. Split both version strings on the '.' delimiter to obtain arrays of revision strings.
  2. Determine the maximum length between the two revision arrays.
  3. Iterate from index 0 to maximum length, convert the revision at the current index to an integer (defaulting to 0 if the index is out of bounds).
  4. Compare the integers from both arrays:
    • If a revision from version1 is greater than the corresponding revision from version2, return 1.
    • If a revision from version1 is less than the corresponding revision from version2, return -1.
  5. If all revisions are equal, then both versions are equivalent and return 0.

This approach uses basic array splitting and a simple loop for comparison, making it both clear and efficient.


Code Solutions

# Define the function to compare version numbers
def compareVersion(version1: str, version2: str) -> int:
    # Split version strings into list of revision strings
    revisions1 = version1.split('.')
    revisions2 = version2.split('.')
    
    # Determine the maximum length between the two lists of revisions
    max_length = max(len(revisions1), len(revisions2))
    
    # Iterate through each revision index
    for i in range(max_length):
        # Convert current revision to integer or use 0 if index is out of bounds
        num1 = int(revisions1[i]) if i < len(revisions1) else 0
        num2 = int(revisions2[i]) if i < len(revisions2) else 0
        
        # Compare the revisions
        if num1 > num2:
            return 1
        if num1 < num2:
            return -1
            
    # If all revisions are equal return 0
    return 0

# Example usage:
print(compareVersion("1.2", "1.10"))  # Output: -1
← Back to All Questions