Skip to main content
Back to problems
Leetcode
Medium
Strings
Arrays
Math
Compare Version Numbers

Compare two software version strings component by component and determine which one is larger.

Acceptance 0%
Problem Statement

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:

  • -1 if version1 < version2
  • 1 if version1 > version2
  • 0 if 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: version1 and version2
  • Each version contains numeric revision parts only

Output Format

  • Return -1, 1, or 0 depending 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
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.