Advertisement

Compare Version Numbers - LeetCode 165 Solution

Compare Version Numbers - Complete Solution Guide

Compare Version Numbers is LeetCode problem 165, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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: If version1 < version2 , return -1. If version1 > version2 , return 1. Otherwise, return 0. Exampl

Detailed Explanation

The problem asks us to compare two version strings, `version1` and `version2`. These strings consist of revisions separated by dots (`.`). We need to compare the revisions numerically, treating missing revisions as 0. Leading zeros in revisions should be ignored (e.g., '1.01' is the same as '1.1'). The function should return -1 if `version1` < `version2`, 1 if `version1` > `version2`, and 0 if they are equal. The version strings contain only digits and dots, and revisions are valid 32-bit integers.

Solution Approach

The solution involves splitting both version strings into arrays of revisions. Then, we iterate through the revisions, comparing corresponding revisions after converting them to integers. If one version string has fewer revisions, we treat the missing revisions as 0. We stop when we find a difference between corresponding revisions or when we have processed all revisions.

Step-by-Step Algorithm

  1. Step 1: Split both version strings `version1` and `version2` into arrays of revision strings using the '.' delimiter.
  2. Step 2: Determine the maximum length of the two revision arrays.
  3. Step 3: Iterate from 0 to `max_len` - 1. In each iteration, get the integer value of the i-th revision from each array. If the index `i` is out of bounds for a particular revision array, treat its value as 0.
  4. Step 4: Compare the integer values of the revisions from `version1` and `version2`. If the revision from `version1` is less than the revision from `version2`, return -1. If it's greater, return 1.
  5. Step 5: If the loop completes without finding any differences, it means the versions are equal. Return 0.

Key Insights

  • Insight 1: The problem requires splitting the version strings into individual revisions.
  • Insight 2: We must handle cases where one version string has fewer revisions than the other by treating missing revisions as 0.
  • Insight 3: Leading zeros need to be ignored when comparing individual revisions.

Complexity Analysis

Time Complexity: O(max(n, m))

Space Complexity: O(n + m)

Topics

This problem involves: Two Pointers, String.

Companies

Asked at: ByteDance, Cisco, Nextdoor, TikTok, Zoho.