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
Input : version1 : 002.0005.12.3 version2 : 188.8.131.52 Output : 0 Input : version1 : 451231654684151546847799885544662 version2 : 1.2184.108.40.206 Output : 1 Input : version1 : 1.21.20 version2 : 1.21.25 Output : -1 Input : version1 : 1.2 version2 : 220.127.116.11.0 Output : 0 Input : version1 : 1.2 version2 : 1.0.1 Output : -1
We have discussed a solution in below post.
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.
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).
This article is contributed by arshpreet soodan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Factorial of a large number
- Calculate sum of all numbers present in a string
- Compare two Version numbers
- Longest common subsequence with permutations allowed
- Check if a string has all characters with same frequency with one variation allowed
- Remainder with 7 for large numbers
- Distinct strings with odd and even changes allowed
- Maximum and minimum sums from two numbers with digit replacements
- Sum of two large numbers
- Check if a large number is divisible by 11 or not
- Difference of two large numbers
- Check if a large number is divisible by 4 or not
- Find the missing number in a string of numbers with no separator
- Multiply Large Numbers represented as Strings
- Divide large number represented as string
Improved By : ayushbehl77