Compare two Version numbers - GeeksforGeeks
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
We strongly recommend you to minimize your browser and try this yourself first.
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.
- C/C++
- Python
C/C++
Read full article from Compare two Version numbers - GeeksforGeeks
No comments:
Post a Comment