KatInterview

165. Compare Version Numbers

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to compare two version strings, version1 and version2. A version string is composed of numerical revisions separated by dots. For example, in 3.1.4, the revisions are 3, 1, and 4.

Solution

Compare Version Numbers in Python

class Solution:
    def compareVersion(self, version1: str, version2: str) -> int:
        n1, n2 = len(version1), len(version2)
        i, j = 0, 0

        # Iterate as long as there are revisions in either string
        while i < n1 or j < n2:
            num1 = 0
            # Parse the next revision number from version1
            while i < n1 and version1[i] != '.':
                num1 = num1 * 10 + int(version1[i])
                i += 1
            # Move pointer past the dot for the next revision
            i += 1

            num2 = 0
            # Parse the next revision number from version2
            while j < n2 and version2[j] != '.':
                num2 = num2 * 10 + int(version2[j])
                j += 1
            # Move pointer past the dot for the next revision
            j += 1

            # Compare the current revisions
            if num1 > num2:
                return 1
            if num1 < num2:
                return -1
            # If revisions are equal, continue to the next one

        # If all revisions are equal, the versions are the same
        return 0

Compare Version Numbers in Java

class Solution {
    public int compareVersion(String version1, String version2) {
        int n1 = version1.length(), n2 = version2.length();
        int i = 0, j = 0;

        // Iterate as long as there are revisions in either string
        while (i < n1 || j < n2) {
            int num1 = 0;
            // Parse the next revision number from version1
            while (i < n1 && version1.charAt(i) != '.') {
                num1 = num1 * 10 + (version1.charAt(i) - '0');
                i++;
            }
            // Move pointer past the dot for the next revision
            i++;

            int num2 = 0;
            // Parse the next revision number from version2
            while (j < n2 && version2.charAt(j) != '.') {
                num2 = num2 * 10 + (version2.charAt(j) - '0');
                j++;
            }
            // Move pointer past the dot for the next revision
            j++;

            // Compare the current revisions
            if (num1 > num2) {
                return 1;
            }
            if (num1 < num2) {
                return -1;
            }
            // If revisions are equal, continue to the next one
        }

        // If all revisions are equal, the versions are the same
        return 0;
    }
}

Compare Version Numbers in C++

class Solution {
public:
    int compareVersion(string version1, string version2) {
        int n1 = version1.length(), n2 = version2.length();
        int i = 0, j = 0;

        // Iterate as long as there are revisions in either string
        while (i < n1 || j < n2) {
            int num1 = 0;
            // Parse the next revision number from version1
            while (i < n1 && version1[i] != '.') {
                num1 = num1 * 10 + (version1[i] - '0');
                i++;
            }
            // Move pointer past the dot for the next revision
            i++; 

            int num2 = 0;
            // Parse the next revision number from version2
            while (j < n2 && version2[j] != '.') {
                num2 = num2 * 10 + (version2[j] - '0');
                j++;
            }
            // Move pointer past the dot for the next revision
            j++;

            // Compare the current revisions
            if (num1 > num2) {
                return 1;
            }
            if (num1 < num2) {
                return -1;
            }
            // If revisions are equal, continue to the next one
        }

        // If all revisions are equal, the versions are the same
        return 0;
    }
};

Compare Version Numbers in TypeScript

function compareVersion(version1: string, version2: string): number {
    const n1 = version1.length;
    const n2 = version2.length;
    let i = 0, j = 0;

    // Iterate as long as there are revisions in either string
    while (i < n1 || j < n2) {
        let num1 = 0;
        // Parse the next revision number from version1
        while (i < n1 && version1[i] !== '.') {
            num1 = num1 * 10 + (version1.charCodeAt(i) - '0'.charCodeAt(0));
            i++;
        }
        // Move pointer past the dot for the next revision
        i++;

        let num2 = 0;
        // Parse the next revision number from version2
        while (j < n2 && version2[j] !== '.') {
            num2 = num2 * 10 + (version2.charCodeAt(j) - '0'.charCodeAt(0));
            j++;
        }
        // Move pointer past the dot for the next revision
        j++;

        // Compare the current revisions
        if (num1 > num2) {
            return 1;
        }
        if (num1 < num2) {
            return -1;
        }
        // If revisions are equal, continue to the next one
    }

    // If all revisions are equal, the versions are the same
    return 0;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.