Algorithm

Compare Version Numbers

Arrays & Strings Pattern

Compare Version Numbers

Given two version strings v1 and v2, compare them. If v1 > v2 return 1; if v1 < v2 return -1; otherwise return 0. Treat missing segments as 0.

CONSTRAINTS
  • 1 <= v1.length, v2.length <= 500
  • v1/v2 consist of digits and '.'
EXAMPLE 1
Input: v1 = "1.01", v2 = "1.001"
Output: 0
Both versions have levels 1 and 1 — the leading zeros in '01' and '001' are cosmetic, so the levels are numerically equal.
EXAMPLE 2
Input: v1 = "1.0", v2 = "1.0.0"
Output: 0
The first two levels match; v1 has no third level, which counts as 0 and matches v2's 0. Equal.
EXAMPLE 3
Input: v1 = "1.10", v2 = "1.2"
Output: 1
The first level ties at 1. The second level is 10 versus 2, and 10 is the larger number, so v1 is greater.
Can I compare the version levels as strings instead of numbers?
No. '10' is greater than '2' numerically but smaller lexicographically, and '01' equals '1' numerically but differs as text. Each level must be parsed to an integer before comparing.
What happens when the two versions have a different number of levels?
Treat every missing level as 0 and compare up to the longer length. So '1.0' equals '1', but '1.0.1' is greater than '1'.
Can a level be larger than a 32-bit integer?
Possibly — a version string can be up to 500 characters. In languages with fixed-width ints, parse into a 64-bit / big integer, or compare digit strings carefully, to avoid overflow.
Are the inputs always well-formed (no empty levels or trailing dots)?
Assume so unless told otherwise — levels are non-negative integers separated by single dots. Worth confirming with the interviewer if the statement is silent on it.

Comparing versions is like navigating a legal document (Section 1.2 vs Section 1.10). Each number between dots is a Revision Level. We compare these levels from left to right, but we cannot use standard string comparison because "1.10" must be considered greater than "1.2".

The Core Challenge: Numerical Values

The "dot-separated" format introduces three tricky scenarios:
1. Leading Zeros: "001" and "1" represent the same revision level (1).
2. Numerical Value: "10" is larger than "2", even though "2" comes after "1" alphabetically.
3. Missing Segments: "1.1" and "1.1.0" represent the same spiritual version. If one version runs out of segments, we treat the missing levels as 0.

The Strategy: Numerical Synchronization

The most robust approach is to iterate through both strings simultaneously and convert each segment into an integer. This automatically handles leading zeros and allows for direct numerical comparisons.

Implementation: The Two-Pointer Parse

Instead of splitting the strings (which uses extra memory), we use two pointers to scan and build integers on the fly. We stop at every dot, compare the values, and continue.

python
i, j = 0, 0
while i < len(version1) or j < len(version2):
    val1, val2 = 0, 0
    
    # Extract numerical value for version1's current segment
    while i < len(version1) and version1[i] != '.':
        val1 = val1 * 10 + int(version1[i])
        i += 1
    
    # Extract numerical value for version2's current segment
    while j < len(version2) and version2[j] != '.':
        val2 = val2 * 10 + int(version2[j])
        j += 1
    
    # Hierarchical check: if levels differ, we found the winner
    if val1 != val2:
        return 1 if val1 > val2 else -1
        
    i += 1; j += 1 # Skip current dots
return 0
Worked Example:v1 = "1.01.2", v2 = "1.1"
1
01
2
1
1
0
We align the version segments. Since Version 2 has only two segments, we pad its third segment with a default value of '0' (shown in gray).
1
01
2
1
1
0
We compare the first segment from both versions: 1 versus 1. Since they are numerically equal, we proceed to the next segment.
1
01
2
1
1
0
We compare the second segment. Version 1 has '01' (which normalizes to 1) and Version 2 has '1' (normalizes to 1). They match.
1
01
2
1
1
0
We compare the third segment: 2 versus 0. Since 2 is greater than 0, Version 1 is greater. We return 1 and terminate.
Interactive Strategy Visualization

version delta analyzer

Segment-wise Numerical Comparison
VERSION 1
1
0
1
VERSION 2
1
0
0
🔍

Comparing Version 1 ("1.0.1") and Version 2 ("1").

O(N + M) Level-wise Numeric Compare