Compare Version Numbers with large inputs allowed
Compare the two versions, version1, version2.
If version1 > version2 return 1
If version1 < version2 return -1
if version1 = version2 return 0
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.
Example of version ordering.
0.1 < 1.1 < 1.2 < 1.13 < 1.13.4
Note : Here the numbers present inside the string can be huge so don’t try to convert these numbers
to unsigned long long. Eg. version1 = 1.234565434523423423523423423423434432.23.0
Examples:
Input : version1 : 002.0005.12.3
version2 : 2.5.12.3
Output : 0
Input : version1 : 451231654684151546847799885544662
version2 : 1.256.24.5.5
Output : 1
Input : version1 : 1.21.20
version2 : 1.21.25
Output : -1
Input : version1 : 1.2
version2 : 1.2.0.0.0
Output : 0
Input : version1 : 1.2
version2 : 1.0.1
Output : -1
We have discussed a solution in below post.
Compare two Version numbers
The previous solution discussed above has problems like, it does not handle leading zeros and does not work for large numbers as individual parts of version numbers are stored as int.
In this solution, above issues are addressed. We traverse the both versions at same time and process them until both of them gets fully traversed. Store the numbers from version1 and version 2 in different strings i.e substr_version1 and substr_version2. Compare these substrings,
if length of substr_version1 > substr_version2 the clearly substr_version1 is greater in value so return +1. Similar is case when substr_version2 > substr_version1, we will return -1. But if both substrings are similar in length then
we will have to check each character from both substrings and then compare those characters and then return the result appropriately.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int compareSubstr( char * substr_version1,
char * substr_version2,
int len_substr_version1,
int len_substr_version2)
{
if (len_substr_version1 > len_substr_version2)
return 1;
else if (len_substr_version1 < len_substr_version2)
return -1;
else {
int i = 0, j = 0;
while (i < len_substr_version1) {
if (substr_version1[i] < substr_version2[j])
return -1;
else if (substr_version1[i]
> substr_version2[j])
return 1;
i++, j++;
}
return 0;
}
}
int compareVersion( char * version1, char * version2)
{
int len_version1 = strlen (version1);
int len_version2 = strlen (version2);
char * substr_version1
= ( char *) malloc ( sizeof ( char ) * 1000);
char * substr_version2
= ( char *) malloc ( sizeof ( char ) * 1000);
int i = 0, j = 0;
while (i < len_version1 || j < len_version2) {
int p = 0, q = 0;
while (version1[i] == '0' )
i++;
while (version2[j] == '0' )
j++;
while (version1[i] != '.' && i < len_version1)
substr_version1[p++] = version1[i++];
while (version2[j] != '.' && j < len_version2)
substr_version2[q++] = version2[j++];
int res = compareSubstr(substr_version1,
substr_version2, p, q);
if (res)
return res;
i++;
j++;
}
return 0;
}
int main()
{
char version1[] = "1.2.032.45" ;
char version2[] = "1.2.32.4" ;
int res = compareVersion(version1, version2);
cout << res << endl;
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compareSubstr( char * substr_version1,
char * substr_version2,
int len_substr_version1,
int len_substr_version2)
{
if (len_substr_version1 > len_substr_version2)
return 1;
else if (len_substr_version1 < len_substr_version2)
return -1;
else {
int i = 0, j = 0;
while (i < len_substr_version1) {
if (substr_version1[i] < substr_version2[j])
return -1;
else if (substr_version1[i]
> substr_version2[j])
return 1;
i++, j++;
}
return 0;
}
}
int compareVersion( char * version1, char * version2)
{
int len_version1 = strlen (version1);
int len_version2 = strlen (version2);
char * substr_version1
= ( char *) malloc ( sizeof ( char ) * 1000);
char * substr_version2
= ( char *) malloc ( sizeof ( char ) * 1000);
int i = 0, j = 0;
while (i < len_version1 || j < len_version2) {
int p = 0, q = 0;
while (version1[i] == '0' )
i++;
while (version2[j] == '0' )
j++;
while (version1[i] != '.' && i < len_version1)
substr_version1[p++] = version1[i++];
while (version2[j] != '.' && j < len_version2)
substr_version2[q++] = version2[j++];
int res = compareSubstr(substr_version1,
substr_version2, p, q);
if (res)
return res;
i++;
j++;
}
return 0;
}
int main()
{
char version1[] = "1.2.032.45" ;
char version2[] = "1.2.32.4" ;
int res = compareVersion(version1, version2);
printf ( "%d\n" , res);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int compareSubstr(String substr_version1,
String substr_version2)
{
int len_substr_version1 = substr_version1.length();
int len_substr_version2 = substr_version2.length();
if (len_substr_version1 > len_substr_version2)
return 1 ;
else if (len_substr_version2 > len_substr_version1)
return - 1 ;
int res
= substr_version1.compareTo(substr_version2);
if (res > 0 )
return 1 ;
else if (res < 0 )
return - 1 ;
return 0 ;
}
public static int compareVersion(String version1,
String version2)
{
String substr_version1[] = version1.split( "[.]" );
String substr_version2[] = version2.split( "[.]" );
int len_version1 = substr_version1.length;
int len_version2 = substr_version2.length;
int i = 0 ;
int j = 0 ;
while (i < len_version1 || j < len_version2) {
String x = "" ;
String y = "" ;
if (i < len_version1) {
if (substr_version1[i].charAt( 0 ) == '0' ) {
int len = substr_version1[i].length();
int k = 0 ;
while (k < len
&& substr_version1[i].charAt(k)
== '0' ) {
k++;
}
x += substr_version1[i].substring(k);
}
else
x += substr_version1[i];
}
if (j < len_version2) {
if (substr_version2[i].charAt( 0 ) == '0' ) {
int len = substr_version2[i].length();
int k = 0 ;
while (k < len
&& substr_version2[i].charAt(k)
== '0' ) {
k++;
}
y += substr_version2[i].substring(k);
}
else
y = substr_version2[i];
}
int res = compareSubstr(x, y);
if (res != 0 )
return res;
i++;
j++;
}
return 0 ;
}
public static void main(String[] args)
{
System.out.println(
compareVersion( "1.2.032.45" , "1.2.32.4" ));
}
}
|
Python3
def compareSubstr(substr_version1, substr_version2):
len_substr_version1 = len (substr_version1)
len_substr_version2 = len (substr_version2)
if (len_substr_version1 > len_substr_version2):
return 1
elif (len_substr_version2 > len_substr_version1):
return - 1
res = substr_version1.compareTo(substr_version2)
if (res > 0 ):
return 1
elif (res < 0 ):
return - 1
return 0
def compareVersion(version1, version2):
substr_version1 = version1.split( "[.]" )
substr_version2 = version2.split( "[.]" )
len_version1 = len (substr_version1)
len_version2 = len (substr_version2)
i = 0
j = 0
while (i < len_version1 or j < len_version2):
x = ""
y = ""
if (i < len_version1):
if (substr_version1[i][ 0 ] = = '0' ):
leng = len (substr_version1[i])
k = 0
while (k < leng and substr_version1[i][k] = = '0' ):
k + = 1
x + = substr_version1[i][k:]
else :
x + = substr_version1[i]
if (j < len_version2):
if (substr_version2[i][ 0 ] = = '0' ):
leng = len (substr_version2[i])
k = 0
while (k < leng and substr_version2[i][k] = = '0' ):
k + = 1
y + = substr_version2[i][k:]
else :
y = substr_version2[i]
res = compareSubstr(x, y)
if (res ! = 0 ):
return res
i + = 1
j + = 1
return 0
if __name__ = = "__main__" :
print (compareVersion( "1.2.032.45" , "1.2.32.4" ))
|
C#
using System;
public class GFG {
public static int compareSubstr( string substr_version1,
string substr_version2)
{
int len_substr_version1 = substr_version1.Length;
int len_substr_version2 = substr_version1.Length;
if (len_substr_version1 > len_substr_version2)
return 1;
else if (len_substr_version2 < len_substr_version1)
return -1;
int res
= substr_version1.CompareTo(substr_version2);
if (res > 0)
return -1;
else if (res < 0)
return 1;
return 0;
}
public static int compareVersion( string version1,
string version2)
{
string [] substr_version1 = version1.Split( "[.]" );
string [] substr_version2 = version2.Split( "[.]" );
int len_version1 = substr_version1.Length;
int len_version2 = substr_version2.Length;
int i = 0;
int j = 0;
while (i < len_version1 || j < len_version2) {
string x = "" ;
string y = "" ;
if (i < len_version1) {
if (substr_version1[i][0] == '0' ) {
int len = substr_version1[i].Length;
int k = 0;
while (k < len
&& substr_version1[i][k]
== '0' ) {
k++;
}
x += substr_version1[i].Substring(k);
}
else
x += substr_version1[i];
}
if (j < len_version2) {
if (substr_version2[i][0] == '0' ) {
int len = substr_version2[i].Length;
int k = 0;
while (k < len
&& substr_version2[i][k]
== '0' ) {
k++;
}
y += substr_version2[i].Substring(k);
}
else
y = substr_version2[i];
}
int res = compareSubstr(x, y);
if (res != 0)
return res;
i++;
j++;
}
return 0;
}
static public void Main()
{
Console.Write(
compareVersion( "1.2.032.45" , "1.2.32.4" ));
}
}
|
Javascript
function compareSubstr(substr_version1, substr_version2) {
let len_substr_version1 = substr_version1.length;
let len_substr_version2 = substr_version2.length;
if (len_substr_version1 > len_substr_version2) {
return 1;
} else if (len_substr_version2 > len_substr_version1) {
return -1;
}
let res = substr_version1.localeCompare(substr_version2);
if (res > 0) {
return 1;
} else if (res < 0) {
return -1;
}
return 0;
}
function compareVersion(version1, version2) {
let substr_version1 = version1.split( "." );
let substr_version2 = version2.split( "." );
let len_version1 = substr_version1.length;
let len_version2 = substr_version2.length;
let i = 0;
let j = 0;
while (i < len_version1 || j < len_version2) {
let x = "" ;
let y = "" ;
if (i < len_version1) {
if (substr_version1[i][0] == "0" ) {
let leng = substr_version1[i].length;
let k = 0;
while (k < leng && substr_version1[i][k] == "0" ) {
k += 1;
}
x += substr_version1[i].substring(k);
} else {
x += substr_version1[i];
}
}
if (j < len_version2) {
if (substr_version2[j][0] == "0" ) {
let leng = substr_version2[j].length;
let k = 0;
while (k < leng && substr_version2[j][k] == "0" ) {
k += 1;
}
y += substr_version2[j].substring(k);
} else {
y = substr_version2[j];
}
}
let res = compareSubstr(x, y);
if (res !== 0) {
return res;
}
i += 1;
j += 1;
}
return 0;
}
console.log(compareVersion( "1.2.032.45" , "1.2.32.4" ));
|
Time Complexity : O(2 * N) –> O(N)
Here worst case is when both the versions are equal so after extracting both substrings each of them will again be compared in compareSubstr() function which will take the complexity upto (2 * N).
Auxiliary Space: O(1) uses constant extra space.
Last Updated :
08 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...