Given two version strings, version1 and version2, compare them. A version string consists of revisions separated by dots '.'. The value of the revision is its integer conversion ignoring leading zeros.
To compare version strings, compare their revision values in left-to-right order. If one of the version strings has fewer revisions, treat the missing revision values as 0.
Return the following:
version1 < version2, return -1.version1 > version2, return 1.Example 1:
Input: version1 = "1.2", version2 = "1.10"
Output: -1
Explanation:
version1's second revision is "2" and version2's second revision is "10": 2 < 10, so version1 < version2.
Example 2:
Input: version1 = "1.01", version2 = "1.001"
Output: 0
Explanation:
Ignoring leading zeroes, both "01" and "001" represent the same integer "1".
Example 3:
Input: version1 = "1.0", version2 = "1.0.0.0"
Output: 0
Explanation:
version1 has less revisions, which means every missing revision are treated as "0".
Constraints:
1 <= version1.length, version2.length <= 500version1 and version2 only contain digits and '.'.version1 and version2 are valid version numbers.version1 and version2 can be stored in a 32-bit integer.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you need to figure out which of two version numbers is newer by reading them both from left to right. The brute force strategy involves breaking down both version numbers into all their individual sections and matching them up step by step until a difference is spotted.
Here's how the algorithm would work step-by-step:
def compareVersion(version1: str, version2: str) -> int:
version_pieces_one = version1.split(".")
version_pieces_two = version2.split(".")
max_length = max(len(version_pieces_one), len(version_pieces_two))
# We pad the shorter version with zeros so both lists have identical lengths for safe comparison
for index in range(max_length):
current_piece_one = int(version_pieces_one[index]) if index < len(version_pieces_one) else 0
current_piece_two = int(version_pieces_two[index]) if index < len(version_pieces_two) else 0
# Immediate return is required because the leftmost differing version piece dictates precedence
if current_piece_one > current_piece_two:
return 1
if current_piece_one < current_piece_two:
return -1
# Reaching this return statement proves every single corresponding piece matched without any difference
return 0To compare two version numbers fairly, we break them down into smaller pieces separated by dots and examine them section by section from left to right. If any section is larger than the corresponding section in the other version, we immediately know which one is newer, and if all sections match, the versions are identical.
Here's how the algorithm would work step-by-step:
def compare_version_numbers(version_string_one, version_string_two):
sections_list_one = version_string_one.split('.')
sections_list_two = version_string_two.split('.')
max_sections_length = max(len(sections_list_one), len(sections_list_two))
for section_index in range(max_sections_length):
# Missing sections default to zero to ensure fair alignment between versions of unequal lengths.
current_value_one = int(sections_list_one[section_index]) if section_index < len(sections_list_one) else 0
current_value_two = int(sections_list_two[section_index]) if section_index < len(sections_list_two) else 0
# Mathematical comparison determines relative newness immediately upon finding the first differing section.
if current_value_one > current_value_two:
return 1
if current_value_one < current_value_two:
return -1
# Identical values across all parsed sections mean both version numbers represent the exact same release.
return 0| Case | How to Handle |
|---|---|
| Versions with differing lengths of revision blocks like 1.01 and 1 | Treat missing revisions as zero during the left-to-right comparison loop. |
| Revisions containing multiple leading zeros like 1.0003 and 1.3 | Parse each revision substring into a standard integer to naturally strip leading zeros. |
| Extremely long version strings with many dot-separated revisions | Use a two-pointer string traversal approach instead of allocating heavy memory arrays for splitting. |
| Version strings with trailing dots like 1.0. or 1..1 | Treat empty segments between consecutive or trailing dots as a value of zero. |
| Version numbers with extremely large revision integers exceeding standard limits | Accumulate parsed values using 64-bit integer types to prevent arithmetic overflow. |
| Completely identical version strings such as 7.5.2 and 7.5.2 | Traverse both strings fully, find all revisions equal, and return zero at the end. |