Problem
Section titled “Problem”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, return1. - Otherwise, return
0.
Example 1:
Input: version1 = "1.2", version2 = "1.10"Output: -1Explanation: version1's second revision is "2" and version2's second revision is "10": 2 < 10, soversion1 < version2.Example 2:
Input: version1 = "1.01", version2 = "1.001"Output: 0Explanation: Ignoring leading zeroes, both "01" and "001" represent the same integer "1".Example 3:
Input: version1 = "1.0", version2 = "1.0.0.0"Output: 0Explanation: version1 has less revisions, which means every missing revision are treated as "0".Constraints:
-
version1.length,version2.length version1andversion2only contain digits and'.'.version1andversion2are valid version numbers.- All the given revisions in
version1andversion2can be stored in a 32-bit integer.
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Revisions must be compared as numbers, not as text: "10" > "2" numerically even though '1' < '2' as characters, and "01" equals "001". Splitting both strings into integer lists works but needs extra memory. We do not need the lists — only the current pair of revisions — so we can parse both strings side by side with one pointer each:
- read the digits of the next revision of each string, building its integer value (leading zeros contribute nothing:
0 * 10 + 0 = 0); - a string that has run out of revisions simply yields
0, which is exactly the “missing revisions are0” rule; - the first pair of unequal revisions decides the answer.
Algorithm
Section titled “Algorithm”- Set
i = 0(intoversion1) andj = 0(intoversion2). - While
i < norj < m:- Parse revision
afromversion1: whilei < nandversion1[i] != '.', doa = a * 10 + digitand advancei. Ifi >= nalready,astays0. - Parse revision
bfromversion2the same way withj. - If
a != b, return-1ifa < b, else1. - Advance
iandjby one to skip the dots.
- Parse revision
- Every revision matched: return
0.
For Example 1: the first round reads a = 1, b = 1; the second reads a = 2, b = 10, and 2 < 10 returns -1.
Complexity
Section titled “Complexity”- Time: — every character of both strings is read once.
- Space: — two indices and two integers.
Edge cases
Section titled “Edge cases”- Different numbers of revisions (Example 3): the shorter string keeps producing
0until the longer one is exhausted. - Leading zeros (Example 2): they add nothing to the value, so
"01"and"001"both parse to1. - Overflow: each revision fits in a 32-bit integer, and the running value never exceeds the final value, so
intis safe.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: int compareVersion(string version1, string version2) { int i = 0, j = 0, n = version1.size(), m = version2.size(); while (i < n || j < m) { int a = 0, b = 0; while (i < n && version1[i] != '.') a = a * 10 + (version1[i++] - '0'); while (j < m && version2[j] != '.') b = b * 10 + (version2[j++] - '0'); if (a != b) return a < b ? -1 : 1; i++; // skip the '.' j++; } return 0; }};class Solution { public int compareVersion(String version1, String version2) { int i = 0, j = 0, n = version1.length(), m = version2.length(); while (i < n || j < m) { int a = 0, b = 0; while (i < n && version1.charAt(i) != '.') a = a * 10 + (version1.charAt(i++) - '0'); while (j < m && version2.charAt(j) != '.') b = b * 10 + (version2.charAt(j++) - '0'); if (a != b) return a < b ? -1 : 1; i++; // skip the '.' j++; } return 0; }}class Solution: def compareVersion(self, version1: str, version2: str) -> int: i = j = 0 n, m = len(version1), len(version2) while i < n or j < m: a = b = 0 while i < n and version1[i] != '.': a = a * 10 + int(version1[i]) i += 1 while j < m and version2[j] != '.': b = b * 10 + int(version2[j]) j += 1 if a != b: return -1 if a < b else 1 i += 1 # skip the '.' j += 1 return 0Give it a try first
Attempt the problem on LeetCode — the code will still be here.