Compare two software version strings component by component and determine which one is larger.
Problem
You are given two version strings, version1 and version2. Each version is made of one or more numeric revision parts separated by dots (.).
Compare the versions from left to right, treating missing revision parts as 0.
Return:
-1ifversion1 < version21ifversion1 > version20if they are equal
A revision part should be compared as an integer value, so leading zeros do not change its meaning.
Notes
- Versions may have different lengths.
- Trailing zero parts do not affect the final result.
- You should compare revision by revision until a difference is found or both versions are exhausted.
Input Format
- Two dot-separated version strings:
version1andversion2 - Each version contains numeric revision parts only
Output Format
- Return
-1,1, or0depending on the ordering of the two versions
Constraints
- Revision parts are non-negative integers
- Leading zeros may appear in a revision part
- Missing revision parts are treated as
0 - Compare using integer value, not lexicographic string order
Example 1
Input
version1 = "1.01", version2 = "1.001"
Output
0
Explanation
Both versions represent the parts [1, 1], so they are equal.
Example 2
Input
version1 = "1.0", version2 = "1.0.0"
Output
0
Explanation
Missing parts are treated as 0, so both versions are equal.
Show 1 more example
Example 3
Input
version1 = "0.1", version2 = "1.1"
Output
-1
Explanation
At the first differing part, 0 < 1.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.