Skip to content

Practice Compare Version Numbers

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#165
LeetCode Problem

Compare Version Numbers

Problem Statement

Compare version1 and version2 by reading their dot-separated revision numbers from left to right. Interpret each revision as an integer, ignoring leading zeros, and treat missing trailing revisions as zero. Return -1 when version1 is smaller, 1 when it is larger, and 0 when the versions are equivalent.

Source: Compare Version Numbers on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
version1 = "1.2", version2 = "1.10"
Output
-1

# Example 2

Input
version1 = "1.01", version2 = "1.001"
Output
0

# Example 3

Input
version1 = "1.0", version2 = "1.0.0.0"
Output
0

Constraints

1 <= version1.length, version2.length <= 500
version1 and version2 only contain digits and '.'.
version1 and version2 are valid version numbers.
All the given revisions in version1 and version2 can be stored in a 32-bit integer.

Related LeetCode Problems

Practice more problems similar to Compare Version Numbers.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.