HomeLeetcode165. Compare Version Numbers - Leetcode Solutions

165. Compare Version Numbers – Leetcode Solutions

Description

Given two version stringsversion1 and version2, compare them. A version string consists of revisions separated by dots '.'. The value of the revision is its integer conversion ignoring leading zeros.

To compare version strings, compare their revision values in left-to-right order. If one of the version strings has fewer revisions, treat the missing revision values as 0.

Return the following:

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

Examples:

Example 1:

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

Output: -1

Explanation:

version1's second revision is "2" and version2's second revision is "10": 2 < 10, so version1 < version2.

Example 2:

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

Output: 0

Explanation:

Ignoring leading zeroes, both "01" and "001" represent the same integer "1".

Example 3:

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

Output: 0

Explanation:

version1 has less revisions, which means every missing revision are treated as "0".

Solution in Python

Python
class Solution:
    def compareVersion(self, version1: str, version2: str) -> int:
        # Split the version strings by the dot separator to get individual revisions
        v1_parts = version1.split('.')
        v2_parts = version2.split('.')
        
        # Determine the maximum length of the version parts
        max_length = max(len(v1_parts), len(v2_parts))
        
        # Compare each revision from both version strings
        for i in range(max_length):
            # Convert the current revision to an integer, default to 0 if the revision is missing
            v1_revision = int(v1_parts[i]) if i < len(v1_parts) else 0
            v2_revision = int(v2_parts[i]) if i < len(v2_parts) else 0
            
            # Compare the two revisions
            if v1_revision < v2_revision:
                return -1
            elif v1_revision > v2_revision:
                return 1
        
        # If all revisions are equal, return 0
        return 0

Explanation:

  1. Splitting the Version Strings:
    • The split('.') method divides the version strings into a list of revisions based on the dot '.' separator.
  2. Determine the Maximum Length:
    • max_length is calculated to ensure that we can iterate over all revisions from both version strings.
  3. Iterate and Compare Revisions:
    • Loop through each revision index up to max_length.
    • Convert each revision to an integer. If a version string has fewer revisions, treat the missing revision as 0 using the conditional expression:
      v1_revision = int(v1_parts[i]) if i < len(v1_parts) else 0
      v2_revision = int(v2_parts[i]) if i < len(v2_parts) else 0
    • Compare the revisions:
      • If v1_revision is less than v2_revision, return -1.
      • If v1_revision is greater than v2_revision, return 1.
  4. Return 0 if Versions are Equal:
    • If the loop completes without finding any unequal revisions, return 0.

Solution in Javascript

JavaScript
/**
 * @param {string} version1
 * @param {string} version2
 * @return {number}
 */
var compareVersion = function(version1, version2) {
    // Split the version strings by the dot separator to get individual revisions
    let v1Parts = version1.split('.');
    let v2Parts = version2.split('.');

    // Determine the maximum length of the version parts
    let maxLength = Math.max(v1Parts.length, v2Parts.length);

    // Compare each revision from both version strings
    for (let i = 0; i < maxLength; i++) {
        // Convert the current revision to an integer, default to 0 if the revision is missing
        let v1Revision = i < v1Parts.length ? parseInt(v1Parts[i], 10) : 0;
        let v2Revision = i < v2Parts.length ? parseInt(v2Parts[i], 10) : 0;

        // Compare the two revisions
        if (v1Revision < v2Revision) {
            return -1;
        } else if (v1Revision > v2Revision) {
            return 1;
        }
    }

    // If all revisions are equal, return 0
    return 0;
};

Solution in Java

Java
class Solution {
    public int compareVersion(String version1, String version2) {
        // Split the version strings by the dot separator to get individual revisions
        String[] v1Parts = version1.split("\\.");
        String[] v2Parts = version2.split("\\.");

        // Determine the maximum length of the version parts
        int maxLength = Math.max(v1Parts.length, v2Parts.length);

        // Compare each revision from both version strings
        for (int i = 0; i < maxLength; i++) {
            // Convert the current revision to an integer, default to 0 if the revision is missing
            int v1Revision = i < v1Parts.length ? Integer.parseInt(v1Parts[i]) : 0;
            int v2Revision = i < v2Parts.length ? Integer.parseInt(v2Parts[i]) : 0;

            // Compare the two revisions
            if (v1Revision < v2Revision) {
                return -1;
            } else if (v1Revision > v2Revision) {
                return 1;
            }
        }

        // If all revisions are equal, return 0
        return 0;
    }
}

Solution in C#

C#
public class Solution {
    public int CompareVersion(string version1, string version2) {
        // Split the version strings by the dot separator to get individual revisions
        string[] v1Parts = version1.Split('.');
        string[] v2Parts = version2.Split('.');

        // Determine the maximum length of the version parts
        int maxLength = Math.Max(v1Parts.Length, v2Parts.Length);

        // Compare each revision from both version strings
        for (int i = 0; i < maxLength; i++) {
            // Convert the current revision to an integer, default to 0 if the revision is missing
            int v1Revision = i < v1Parts.Length ? int.Parse(v1Parts[i]) : 0;
            int v2Revision = i < v2Parts.Length ? int.Parse(v2Parts[i]) : 0;

            // Compare the two revisions
            if (v1Revision < v2Revision) {
                return -1;
            } else if (v1Revision > v2Revision) {
                return 1;
            }
        }

        // If all revisions are equal, return 0
        return 0;
    }
}

Subscribe
Notify of

0 Comments
Inline Feedbacks
View all comments

Popular