Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Compare Version Numbers

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "165" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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. 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 <= 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.

Explanation

Here's a breakdown of the approach, complexity, and Python code:

  • High-Level Approach:

    • Split each version string into a list of revisions using the '.' delimiter.
    • Iterate through the revisions, comparing the corresponding revision values as integers, handling potential IndexError exceptions by treating missing revisions as 0.
    • Return -1, 1, or 0 based on the comparison results.
  • Complexity:

    • Runtime Complexity: O(max(n, m)), where n and m are the number of revisions in version1 and version2, respectively.
    • Storage Complexity: O(n + m) due to the space used for storing the split revisions of both version strings. Could be optimized to O(1) by iterating using two pointers and not storing the split versions. The solution below stores the split versions for readability.

Code

    def compareVersion(version1: str, version2: str) -> int:
    """
    Compares two version strings.

    Args:
        version1: The first version string.
        version2: The second version string.

    Returns:
        -1 if version1 < version2, 1 if version1 > version2, and 0 if they are equal.
    """
    v1 = version1.split('.')
    v2 = version2.split('.')

    max_len = max(len(v1), len(v2))

    for i in range(max_len):
        rev1 = 0
        rev2 = 0

        if i < len(v1):
            rev1 = int(v1[i])
        if i < len(v2):
            rev2 = int(v2[i])

        if rev1 < rev2:
            return -1
        elif rev1 > rev2:
            return 1

    return 0

More from this blog

C

Chatmagic blog

2894 posts