A version number is a string which is used to identify unique states of a software product. A version number looks like a.b.c.d where a, b etc are number, so version number is a string in which numbers are separated by dots. These number generally represent hierarchy from major to minor (a is major and d is minor).
In this problem we are given two version numbers, we need to compare them and conclude which one is lates version number (that is which version number is smaller).
For example if V1 = “1.0.31” V2 = “1.0.27” Then V2 version is latest (or smaller) because V2 < V1
It is not possible to compare them directly because of dot but we can compare them numeric part wise and then we can check which version is latest. In below code such a method is implemented which traverse through strings, separates numeric part and compare them, if equal go for next numeric part and so on until they differ otherwise flag them as equal.
In below code, a method is implemented to compare two versions, If we have more than two version then below versionCompare method can be used as cmp method of sort method, which will sort all versions according to specified comparison.
1.0.3 is smaller
This article is contributed by Utkarsh Trivedi. 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.
- Calculate sum of all numbers present in a string
- Remainder with 7 for large numbers
- Maximum and minimum sums from two numbers with digit replacements
- Sum of two large numbers
- Difference of two large numbers
- Find the missing number in a string of numbers with no separator
- Multiply Large Numbers represented as Strings
- Calculate maximum value using '+' or '*' sign between two numbers in a string
- Find numbers of balancing positions in string
- Longest common substring in binary representation of two numbers
- Find position of the given number among the numbers made of 4 and 7
- Print N-bit binary numbers having more 1’s than 0’s in all prefixes
- Compare two strings lexicographically in Java
- Count numbers in range such that digits in it and it's product with q are unequal
- Compare Version Numbers with large inputs allowed