LeetCode – Compare Version Numbers (Java)

Problem

Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character. The . character does not represent a decimal point and is used to separate number sequences. Here is an example of version numbers ordering:

0.1 < 1.1 < 1.2 < 13.37

Java Solution

The tricky part of the problem is to handle cases like 1.0 and 1. They should be equal.

public int compareVersion(String version1, String version2) {
    String[] arr1 = version1.split("\\.");
    String[] arr2 = version2.split("\\.");
 
    int i=0;
    while(i<arr1.length || i<arr2.length){
        if(i<arr1.length && i<arr2.length){
            if(Integer.parseInt(arr1[i]) < Integer.parseInt(arr2[i])){
                return -1;
            }else if(Integer.parseInt(arr1[i]) > Integer.parseInt(arr2[i])){
                return 1;
            }
        } else if(i<arr1.length){
            if(Integer.parseInt(arr1[i]) != 0){
                return 1;
            }
        } else if(i<arr2.length){
           if(Integer.parseInt(arr2[i]) != 0){
                return -1;
            }
        }
 
        i++;
    }
 
    return 0;
}

16 thoughts on “LeetCode – Compare Version Numbers (Java)”

  1. private static int compareVersion(String version1, String version2){
    String[] ver1 = version1.split(“\.”);
    String[] ver2 = version2.split(“\.”);
    int len1 = ver1.length;
    int len2 = ver2.length;
    for(int i=0;iInteger.valueOf(ver2[i])){
    return 1;
    }else if(Integer.valueOf(ver1[i])len2){
    return 1;
    }else if(len1 < len2){
    return -1;
    }
    return 0;
    }

  2. In the site’s solution, 1.01 == 1.1, which I think might not be the intention in the question.
    In my solution, as in the original, 1 < 1.0.
    Providing an alternative solution:

    int compare(String v1, String v2)
    {
    if (v1 == null || v2 == null) throw new NullPointerException(“Version cannot be null”);

    /* TODO: Use a split iterator to not calculate everything in advance. */
    String[] args1 = v1.split(‘.’);
    String[] args2 = v2.split(‘.’);

    for (int i = 0 ; i < args1.length && i args2.length ? 1 : -1;
    }

  3. Hi When we have version like v1 = “1236243535539484.352453542553.243525532435” and v2 = “1236243535539484.352453542553.243525532436” then how this will work? In this case we will get Integer limit exceed issue.

  4. Hi, I dealt with this problem as well. I wrote an Open Source library that contains several utils and one of them is working with versions – Converting Strings to Version class with validation, Version comparison, version ranges and so forth. Here is the link to the article that describes the library and where to get it: https://community.oracle.com/blogs/michaelgantman/2016/01/26/open-source-java-library-with-stacktrace-filtering-silent-string-parsing-and-version-comparison. (See paragraph

    Compare Versions
    Also here is a link to javadoc: http://michaelgantman.github.io/Mgnt/docs/

  5. Hi,

    I am here for the coding interview preparation and here is the solution that works for all the situations.
    I am simply conditioning the input and using String.comparedTo(String). It works for all the given scenarios. Here is my code


    public static int compareVersion(String ver1, String ver2)
    {
    if(!ver1.contains("."))
    {
    ver1 = ver1 + ".0";
    }

    if(ver1.startsWith("."))
    {
    ver1 = "0" + ver1;
    }
    if(ver1.endsWith("."))
    {
    ver1 = ver1+"0";
    }

    if(!ver2.contains("."))
    {
    ver2 = ver2 + ".0";
    }

    if(ver2.startsWith("."))
    {
    ver2 = "0" + ver2;
    }
    if(ver2.endsWith("."))
    {
    ver2 = ver2+"0";
    }

    //Now ver1 and ver2 is in x.y.z.p format
    System.out.println("Version 1 = " + ver1 + ", Version2 = " + ver2);

    //To fulfill the requirements
    int result = ver1.compareTo(ver2) ;

    if(result > 0 ) {result = 1;}
    if(result < 0 ) {result = -1;}

    return result;
    }

  6. What if version1 = 2.1.1.0 and version2 = 2.1.1.0.0.0.1; This won’t work as the fifth digit is in fact = 0. Hence it is not going to return -1 even though it should.

    else if(i<arr2.length){
    if(Integer.parseInt(arr2[i]) != 0){
    return -1;

  7. Simply Use a regex to remove the dot and then parse the string to int and compare them.


    String version1 ="1.2";
    String version2="0.2"

    //Now Use regex to remove the dot, convert to integer and compare

    int intVersion1 = Integer.pareseInt(version1.replaceAll("[-+.^:,]",""));
    int intVersion2 = Integer.pareseInt(version2.replaceAll("[-+.^:,]",""));

    if(intVersion1 > intVersion2)
    //version 1 is biiger
    else
    // version2 is bigger

    // Also works with multiple decimals.
    If version is something like , 12.2.33 and so on

Leave a Comment