# Leetcode Compare Version Number problem solution

In this Leetcode Compare Version Number problem solution we have 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:

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

## Problem solution in Python.

```def compareVersion(self, version1, version2):
v1 = [int(n) for n in version1.split('.')]
v2 = [int(n) for n in version2.split('.')]
size = max(len(v1), len(v2))

for i in range(size):
n1 = v1[i] if i < len(v1) else 0
n2 = v2[i] if i < len(v2) else 0
if n1 > n2:
return 1
if n1 < n2:
return -1

return 0
```

## Problem solution in Java.

```String arr1[] = version1.split("\\.");
String arr2[] = version2.split("\\.");

int len1 = arr1.length;
int len2 = arr2.length;
int i = 0, j = 0;
System.out.println(len1 + "..." + len2);
while (i < len1 && j < len2) {
int num1 = Integer.parseInt(arr1[i]);
int num2 = Integer.parseInt(arr2[i]);
i++;
j++;
if (num1 < num2) {
return -1;
} else if (num2 < num1) {
return 1;
}
}
if (len1 > len2) {
while (i < len1) {
if (Integer.parseInt(arr1[i]) > 0) {
return 1;
}
i++;
}

}
if (len2 > len1) {
while (j < len2) {
if (Integer.parseInt(arr2[j]) > 0) {
return -1;
}
j++;
}

}

return 0;
```

## Problem solution in C++.

```int compareVersion(string version1, string version2) {
int result;
queue<int> q1, q2;
stringstream ss1(version1), ss2(version2);
string token;
while(getline(ss1, token, '.')){
q1.push(stoi(token));
}
while(getline(ss2, token, '.')){
q2.push(stoi(token));
}
while(!q1.empty() && !q2.empty()){
if(q1.front() == q2.front()){
q1.pop(); q2.pop();
}else{
if(q1.front() > q2.front()) return 1;
else return -1;
}
}
if(q1.empty() && q2.empty()) return 0;
while(!q1.empty()){
if(q1.front() > 0) return 1;
else q1.pop();
}
while(!q2.empty()){
if(q2.front() > 0) return -1;
else q2.pop();
}
return 0;
}
```

## Problem solution in C.

```int get_nr(char *str, int slen, int *idx) {
int nr = 0, neg = 0;

while (*idx < slen && *(str + *idx) != '.') {
if (*(str + *idx) == '-') {
neg = 1;
++*idx;
continue;
}
nr *= 10;
nr += *(str + *idx) - '0';
++*idx;
}

if (*(str + *idx) == '.')
++*idx;

if (neg)
nr *= -1;

return nr;
}

int compareVersion(char* v1, char* v2) {
int idx1 = 0, idx2 = 0;
int nr1 = 0, nr2 = 0;
int len1 = strlen(v1);
int len2 = strlen(v2);

while (idx1 < len1 || idx2 < len2) {

nr1 = 0, nr2 = 0;

nr1 = get_nr(v1, len1, &idx1);
nr2 = get_nr(v2, len2, &idx2);

if (nr1 > nr2)
return 1;
else if (nr1 < nr2)
return -1;
}
return 0;
}
```