A version number is a string that 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 the version number is a string in which numbers are separated by dots. These numbers 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 the latest version number (that is which version number is smaller).
Input: V1 = “1.0.31” V2 = “1.0.27” Output: v2 is latest Because V2 < V1 Input: V1 = “1.0.10” V2 = “1.0.27” Output: v1 is latest Because V1 < V2
Approach: It is not possible to compare them directly because of dot but the versions can compared numeric part wise and then the latest version can be found. Traverse through strings and separate numeric part and compare them, if equal then next numeric part is compared and so on until they differ otherwise flag them as equal.
Implement a method to compare two versions, If there are more than two versions then the below versionCompare method can be used as compare method of sort method, which will sort all versions according to the specified comparison.
1.0.3 is smaller
- Time Complexity: O(n), where n is the length of the string.
Only one traversal of the string is needed.
- Auxiliary space: O(1).
As no extra space is needed.
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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Compare Version Numbers with large inputs allowed
- Defanged Version of Internet Protocol Address
- Compare two strings lexicographically in Java
- Compare two strings considering only alphanumeric characters
- C++ Program to compare two string using pointers
- C++ program to compare two Strings using Operator Overloading
- Sum of two large numbers
- Difference of two large numbers
- Remainder with 7 for large numbers
- Add two numbers represented by two arrays
- BCD addition of given Decimal numbers
- Sum of first K even-length Palindrome numbers
- Print N-bit binary numbers having more 1’s than 0’s in all prefixes
- Calculate sum of all numbers present in a string
- Consecutive sequenced numbers in a string