{
  "$type": "site.standard.document",
  "bskyPostRef": {
    "cid": "bafyreialotjysk7op5uabn7oo2hhxafvzd5zpgvliogqhxmvnbdv47swpi",
    "uri": "at://did:plc:25rdn5elo5izoxrmtis34zuk/app.bsky.feed.post/3mpome5rgfo72"
  },
  "coverImage": {
    "$type": "blob",
    "ref": {
      "$link": "bafkreihywm345avvunhu2c6c4fndcpvnzg74gw2aywtmgyrenf4jy5xeuy"
    },
    "mimeType": "image/webp",
    "size": 46628
  },
  "path": "/jaspreet_singh_86ae1740ac/compare-version-numbers-56n8",
  "publishedAt": "2026-07-02T17:35:39.000Z",
  "site": "https://dev.to",
  "tags": [
    "algorithms",
    "interview",
    "java",
    "leetcode"
  ],
  "textContent": "##  Problem Statement\n\nGiven two version strings:\n\n\n\n    version1\n\n    version2\n\n\nEach version consists of revisions separated by dots (`.`).\n\nCompare the two versions.\n\nReturn:\n\n\n\n    1  → version1 > version2\n\n    -1 → version1 < version2\n\n    0  → Both are equal\n\n\nLeading zeros should be ignored.\n\n##  Brute Force Intuition\n\nIn an interview, you can explain it like this:\n\n> Split both version strings using `\".\"`, convert each revision to an integer, and compare corresponding revisions one by one.\n\nIf one version has fewer revisions, treat the missing revisions as `0`.\n\n###  Complexity\n\n  * Time Complexity: **O(N + M)**\n  * Space Complexity: **O(N + M)**\n\n\n\n###  Brute Force Code\n\n\n    class Solution {\n\n        public int compareVersion(String version1,\n                                  String version2) {\n\n            String[] v1 = version1.split(\"\\\\.\");\n            String[] v2 = version2.split(\"\\\\.\");\n\n            int n = Math.max(v1.length, v2.length);\n\n            for (int i = 0; i < n; i++) {\n\n                int num1 = i < v1.length\n                        ? Integer.parseInt(v1[i])\n                        : 0;\n\n                int num2 = i < v2.length\n                        ? Integer.parseInt(v2[i])\n                        : 0;\n\n                if (num1 > num2)\n                    return 1;\n\n                if (num1 < num2)\n                    return -1;\n            }\n\n            return 0;\n        }\n    }\n\n\n##  Moving Towards the Optimal Approach\n\nInstead of creating arrays using `split()`,\n\nwe can process both strings directly.\n\nTraverse both strings simultaneously,\n\nextract one revision at a time,\n\nand compare immediately.\n\nThis avoids creating extra arrays.\n\n##  Pattern Recognition\n\nWhenever you see:\n\n  * Dot Separated Values\n  * Version Strings\n  * Sequential Comparison\n\n\n\nThink:\n\n**Two Pointers + String Parsing**\n\n##  Key Observation\n\nEvery revision is simply a number.\n\nRead characters until:\n\n\n\n    '.'\n\n    or\n\n    End of String\n\n\nConvert that revision into an integer.\n\nCompare corresponding revisions.\n\n##  Optimal Approach\n\nMaintain two pointers:\n\n\n\n    i → version1\n\n    j → version2\n\n\nExtract one revision from both strings.\n\nCompare:\n\n\n\n    num1\n\n    vs\n\n    num2\n\n\nIf equal,\n\nmove to the next revision.\n\n##  Optimal Java Solution\n\n\n    class Solution {\n\n        public int compareVersion(String version1,\n                                  String version2) {\n\n            int i = 0;\n            int j = 0;\n\n            while (i < version1.length() ||\n                   j < version2.length()) {\n\n                int num1 = 0;\n\n                while (i < version1.length() &&\n                       version1.charAt(i) != '.') {\n\n                    num1 = num1 * 10\n                         + (version1.charAt(i) - '0');\n\n                    i++;\n                }\n\n                int num2 = 0;\n\n                while (j < version2.length() &&\n                       version2.charAt(j) != '.') {\n\n                    num2 = num2 * 10\n                         + (version2.charAt(j) - '0');\n\n                    j++;\n                }\n\n                if (num1 > num2)\n                    return 1;\n\n                if (num1 < num2)\n                    return -1;\n\n                i++;\n                j++;\n            }\n\n            return 0;\n        }\n    }\n\n\n##  Dry Run\n\n###  Input\n\n\n    version1 = \"1.01\"\n\n    version2 = \"1.001\"\n\n\nCompare:\n\n\n\n    1\n\n    =\n\n    1\n\n\nNext Revision:\n\n\n\n    01\n\n    =\n\n    001\n\n    ↓\n\n    1\n\n    =\n\n    1\n\n\nAnswer:\n\n\n\n    0\n\n\n###  Example 2\n\n\n    version1 = \"1.0\"\n\n    version2 = \"1.0.1\"\n\n\nCompare:\n\n\n\n    1 = 1\n\n    0 = 0\n\n    0 < 1\n\n\nAnswer:\n\n\n\n    -1\n\n\n##  Why This Works?\n\nEach revision is processed exactly once.\n\nInstead of storing all revisions,\n\nwe compare them as soon as they are parsed.\n\nMissing revisions are naturally treated as:\n\n\n\n    0\n\n\nbecause the extracted value remains zero when one version ends.\n\n##  Complexity Analysis\n\nMetric | Complexity\n---|---\nTime Complexity | O(N + M)\nSpace Complexity | O(1)\n\nWhere:\n\n  * `N = version1.length()`\n  * `M = version2.length()`\n\n\n\n##  Interview One-Liner\n\n> Traverse both version strings simultaneously, parse one revision at a time using two pointers, and compare corresponding revision numbers without splitting the strings.\n\n##  Pattern Learned\n\n\n    Delimited String\n\n    ↓\n\n    Parse Number\n\n    ↓\n\n    Compare\n\n    ↓\n\n    Move Forward\n\n\n###  Similar Problems\n\n  * Compare Version Numbers\n  * String to Integer (ATOI)\n  * Roman to Integer\n  * Basic Calculator\n  * Valid Number\n\n\n\n##  Memory Trick\n\nThink:\n\n\n\n    Read Revision\n\n    ↓\n\n    Convert to Number\n\n    ↓\n\n    Compare\n\n    ↓\n\n    Next Revision\n\n\n###  Mental Model\n\n\n    1.0.23\n\n    ↓\n\n    1\n\n    ↓\n\n    0\n\n    ↓\n\n    23\n\n    ↓\n\n    Compare One by One\n\n\nWhenever you hear:\n\n> \"Compare version strings\"\n\nyour brain should immediately think:\n\n**Two Pointers + Parse Each Revision**",
  "title": "Compare version numbers"
}