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;
}
Category >> Algorithms >> Interview  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Vatsal Shah

    What if it’s 1.1.2 and 1.2?

  • crazyman

    19 is bigger than 2
    its not 1.20

  • san25

    Doesn’t work with 1.2 and 1.19.
    Should report 1 but reports -1.

  • akh

    Removing trailing 0s and then remove all . to form one integer:

    Like 1.0.0.1.0 => 1001

  • Devendra Vaja

    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;
    }

  • Snehil Bhatt

    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;

  • Shariq Aziz

    what about something like 1.0 and 1. They should be equal.

  • Prateek Mishra

    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

  • nelson

    In case of multiple decimal points

  • sansor

    Why not use double? and just compare two double values..

  • Anushree Acharjee

    Does not work for input string having value larger than integer range, throws NFE