leetcode Question: Compare Version Numbers

 Compare Version Numbers

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.
For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
Here is an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37

Analysis:

This question is to test the skills of string to int and int to string convention.
In C++, parsing string (e.g., parse string according to ' , ') as well as int to string operation can be done using the stringstream.  In python, it is much easier since we have int() and str() methods to do so.

The idea of this problem could be considered to compare two list of numbers that are separated by '.' Note that when comparing list with different length, if they are the same for the 1st part, if the longer list have all 0s in its tail, then those two lists are the same. E.g.,    1.0.000.0 and 1, two lists generated are [1,0,0,0], and [1]. And it is necessary to check "all" digits instead of checking only the "next" digits because if there exists one digit that is not 0, the two numbers are not equal.  




Code(C++):

class Solution {
public:
    int compareVersion(string version1, string version2) {
        istringstream st1(version1);
        istringstream st2(version2);
        string token;
        vector<int> d1;
        vector<int> d2;
        while (getline(st1,token,'.')){
            stringstream os1;
            os1.str(token);
            int tmp;
            os1 >> tmp;
            d1.push_back(tmp);
        }
        while (getline(st2,token,'.')){
            stringstream os2;
            os2<<token;
            int tmp;
            os2 >> tmp;
            d2.push_back(tmp);
        }
        
        int n1 = d1.size();
        int n2 = d2.size();
        
        for (int i=0;i<min(n1,n2);i++){
            if (d1[i]>d2[i]){ return 1;}
            if (d1[i]<d2[i]){ return -1;}
        }
        
        if (n1<n2){
            for (int i=n1;i<n2;i++){
                if (d2[i]!=0){return -1;}
            }
            return 0;
        }
        if (n1>n2){
            for (int i=n2;i<n1;i++){
                if (d1[i]!=0){return 1;}
            }
            return 0;
        }
        if (n1==n2){return 0;}
    }
};

Code(Python):

class Solution:
    # @param version1, a string
    # @param version2, a string
    # @return an integer
    def compareVersion(self, version1, version2):
        l1 = version1.split('.')
        l2 = version2.split('.')
        for i in range(min(len(l1),len(l2))):
            if int(l1[i]) > int(l2[i]):
                return 1
            if int(l1[i]) < int(l2[i]):
                return -1
                
        if len(l1)<len(l2):
            if all( int(n)==0 for n in l2[len(l1)::]):
                return 0
            else:
                return -1
            
        if len(l1)>len(l2):
            if all( int(n) == 0 for n in l1[len(l2)::]):
                return 0
            else:
                return 1
                
        if len(l2) == len(l2):
            return 0

2 comments:

  1. what if number before first dot is not fitting in int (not even in long long)?? say:
    string version1="444444444444444444444444444444"
    string version1="44444444444444444444444444444444"

    ReplyDelete
    Replies
    1. Question allows such cases and thus your solution will fail
      perfect solution is here ....
      http://qa.geeksforgeeks.org/3709/compare-the-version-numbers

      Delete