Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Compare Version Numbers

XPChallenge Points: 20
levelLevel: Medium

You are given two version strings — version1 and version2.
Each version string consists of revision numbers separated by dots (.).
Each revision is an integer (ignoring any leading zeros).
Your task is to compare the two version numbers and determine which is greater, smaller, or equal.
Rules:
• Compare revisions from left to right.
• If a version has fewer revisions, treat missing ones as 0.
• Return:
o -1 → if version1 < version2
o 1 → if version1 > version2
o 0 → if both are equal

Example 1:

Input: version1 = "2.4" version2 = "2.10"

Output: -1

Example 2:

Input: version1 = "3.01" version2 = "3.001"

Output: 0

Example 3:

Input: version1 = "5.0" version2 = "5.0.0.0"

Output: 0

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result