Compare Version Numbers


Problem Statement :


Given two version numbers, version1 and version2, compare them.

Version numbers consist of one or more revisions joined by a dot '.'. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character. Revisions are 0-indexed from left to right, with the leftmost revision being revision 0, the next revision being revision 1, and so on. For example 2.5.33 and 0.1 are valid version numbers.

To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0. For example, version 1.0 is less than version 1.1 because their revision 0s are the same, but their revision 1s are 0 and 1 respectively, and 0 < 1.

Return the following:

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

Example 1:

Input: version1 = "1.01", version2 = "1.001"
Output: 0
Explanation: Ignoring leading zeroes, both "01" and "001" represent the same integer "1".
Example 2:

Input: version1 = "1.0", version2 = "1.0.0"
Output: 0
Explanation: version1 does not specify revision 2, which means it is treated as "0".
Example 3:

Input: version1 = "0.1", version2 = "1.1"
Output: -1
Explanation: version1's revision 0 is "0", while version2's revision 0 is "1". 0 < 1, so version1 < version2.
 

Constraints:

1 <= version1.length, version2.length <= 500
version1 and version2 only contain digits and '.'.
version1 and version2 are valid version numbers.
All the given revisions in version1 and version2 can be stored in a 32-bit integer.



Solution :



title-img


                            Solution in C :

int countDot(char* s, int n){
    int dot = 0 ;
    for(int i = 0; i < n; i++){
        if(s[i] == '.')
            dot++;
    }
    return dot ;
}

void helper(char* s, int n, int* data){
    unsigned int val = 0 ; 
    int idx = 0 ;
    for(int i = 0; i < n; i++){
        if(s[i] == '.'){
            data[idx] = val ;
            idx++;
            val = 0;
        }
        else{
            val = val*10 + s[i] - '0' ;
        }
    }
    data[idx] = val ;
}
int compareVersion(char * version1, char * version2){
    int n1 = strlen(version1) ;
    int n2 = strlen(version2) ;
    int item1 = countDot(version1, n1) + 1 ;
    int item2 = countDot(version2, n2) + 1;
    int* data1 = malloc( item1 * sizeof(int)) ;
    int* data2 = malloc( item2 * sizeof(int)) ;
    helper(version1, n1, data1) ;
    helper(version2, n2, data2) ;
    int p1 = 0, p2 = 0 ;
    int ans = 0 ;
    while(p1 < item1 || p2 < item2){
        if(p1 == item1){
            for(int i = p2; i < item2; i++){
                if(data2[i] != 0){
                    ans = -1 ;
                    goto exit ;
                }
            }
            ans = 0 ;
            goto exit ;
        }
        else if(p2 == item2){
            for(int i = p1; i < item1; i++){
                if(data1[i] != 0){
                    ans = 1 ;
                    goto exit ;
                }
            }
            ans = 0 ;
            goto exit ;
        }
        else{
            if(data1[p1] > data2[p2]){
                ans = 1 ;
                goto exit ;
            }
            if(data1[p1] < data2[p2]){
                ans = -1 ;
                goto exit ;
            }
            p1++ ;
            p2++;
        }     
    }
    exit :
    free(data1) ;
    free(data2) ;
    return ans ;
}
                        


                        Solution in C++ :

class Solution {
 public:
  int compareVersion(string version1, string version2) {
    istringstream iss1(version1);
    istringstream iss2(version2);
    int v1;
    int v2;
    char dotChar;

    while (bool(iss1 >> v1) + bool(iss2 >> v2)) {
      if (v1 < v2)
        return -1;
      if (v1 > v2)
        return 1;
      iss1 >> dotChar;
      iss2 >> dotChar;
      v1 = 0;
      v2 = 0;
    }

    return 0;
  };
};
                    


                        Solution in Java :

class Solution {
    int v1ptr=0;
    int v2ptr=0;
    public int getNextVersion(String version,int i){
        int x=0;
        int mode=i;
        if(i==0){
            i=v1ptr;
        }else{
            i=v2ptr;
        }

        int j=i;
        for(j=i;j<version.length();j++){
            char b=version.charAt(j);
            if(b=='.'){
                break;
            }else{
                x=x*10+(b-'0');
            }
        }

        if(mode==0){
            v1ptr=j+1;
        }else{
            v2ptr=j+1;
        }
        return x;
    }
    public int compareVersion(String version1, String version2) {
        while(v1ptr<version1.length() || v2ptr<version2.length()){
            int a=getNextVersion(version1,0);
            int b=getNextVersion(version2,1);

            if(a>b){
                return 1;
            }else if(b>a){
                return -1;
            }else{

            }
        }
        return 0;
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def compareVersion(self, version1: str, version2: str) -> int:
        v1,v2=version1.split('.'),version2.split('.')
        flag=1
        if len(v1)>len(v2):
            v1,v2=v2,v1
            flag=-1
        for i in range(len(v1)):
            a,b=int(v1[i]),int(v2[i])
            if a<b:return -1*flag
            elif a>b:return 1*flag
        for j in range(i+1,len(v2)):
            if int(v2[j])!=0:return 1*flag*-1
        return 0
                    


View More Similar Problems

Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

View Solution →

Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

View Solution →

Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

View Solution →

Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →

Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →

Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →