Tap here to switch tabs
Problem
Ask
Submissions

Problem: Compare Version Numbers

med
30 min
Understand how to compare version strings by breaking them into revisions and using two pointers to evaluate their integer values. Learn to handle leading zeros and unequal lengths to determine if one version is greater, lesser, or equal to another.

Statement

Given two version strings, version1 and version2, compare them. A version string is composed of revisions separated by dots ('.'). Each revision’s value is determined by converting it to an integer, disregarding any leading zeros.

Compare the two version strings by evaluating their revision values from left to right. If one version string contains fewer revisions than the other, treat each missing revision as 0.

Return the result of the comparison as follows:

  • Return 1-1 if version1 is less than version2.

  • Return 11 if version1 is greater than version2.

  • Return 00 if both versions are equal.

Note: Each revision value in version1 and version2 is guaranteed to fit within a 3232-bit integer.

Constraints:

  • 11 \leq version1.length, version2.length 500\leq 500

  • version1 and version2 contain only digits and '.'

  • version1 and version2 are valid version numbers

  • All given revisions in version1 and version2 can be stored in a 3232-bit integer

Tap here to switch tabs
Problem
Ask
Submissions

Problem: Compare Version Numbers

med
30 min
Understand how to compare version strings by breaking them into revisions and using two pointers to evaluate their integer values. Learn to handle leading zeros and unequal lengths to determine if one version is greater, lesser, or equal to another.

Statement

Given two version strings, version1 and version2, compare them. A version string is composed of revisions separated by dots ('.'). Each revision’s value is determined by converting it to an integer, disregarding any leading zeros.

Compare the two version strings by evaluating their revision values from left to right. If one version string contains fewer revisions than the other, treat each missing revision as 0.

Return the result of the comparison as follows:

  • Return 1-1 if version1 is less than version2.

  • Return 11 if version1 is greater than version2.

  • Return 00 if both versions are equal.

Note: Each revision value in version1 and version2 is guaranteed to fit within a 3232-bit integer.

Constraints:

  • 11 \leq version1.length, version2.length 500\leq 500

  • version1 and version2 contain only digits and '.'

  • version1 and version2 are valid version numbers

  • All given revisions in version1 and version2 can be stored in a 3232-bit integer