codejump
MediumPro challengePythonJavaScriptTypeScript

Strings — Compare Version Numbers

AlgorithmsStringsTwo PointersParsing

solve(version1, version2) — each version is a list of non-negative integer revisions
joined by dots ("1.2.10"). Compare them revision by revision from left to right and return
-1 if version1 < version2, 1 if version1 > version2, and 0 if they are equal.

Two traps: revisions are compared as integers ("10" is greater than "2", and "01"
equals "1", so leading zeros are ignored), and a missing revision counts as 0
("1.0" equals "1.0.0" and "1").

Examples

solve("1.01", "1.001")       → 0
solve("1.0", "1.0.0")        → 0
solve("0.1", "1.1")          → -1
solve("1.2", "1.10")         → -1
solve("1.0.1", "1")          → 1

Sample tests

Test #1Leading zeros ignored
Input: ["1.01","1.001"]
Output: 0
Test #2Missing revision is zero
Input: ["1.0","1.0.0"]
Output: 0
Test #3Smaller first revision
Input: ["0.1","1.1"]
Output: -1