Java String Program · S44
Compare Version Numbers in Java
Versions are compared number by number, not character by character: 2.10 is newer than 2.5. Missing parts count as zero, so 1.0.0 equals 1.
Problem Statement
Given two version strings made of non-negative integers separated by dots, return -1 if the first is older, 1 if it is newer and 0 if they are equal.
Compare the parts from left to right as integers, so leading zeros are ignored. A version with fewer parts is padded with zeros.
Input/Output Examples
"2.5" vs "2.10"Output-1Explanation: 5 < 10, even though "5" > "1" as text."1.0.0" vs "1"Output0Explanation: 1 is treated as 1.0.0."1.02" vs "1.2"Output0Explanation: 02 and 2 are the same number.Visual Explanation
- Part 1values222 vs 2 equal, continue
- Part 2values5105 vs 10 5 < 10
- Donefirst difference decides-1
"2.5" and "2.10": the first parts are equal, so the second parts decide. As numbers, 5 is less than 10.The first part that differs decides the result, and later parts are never read. Treating missing parts as 0 lets versions of different lengths be compared with the same loop.
Approaches
The first approach splits the strings and parses each part. The second reads the numbers directly with two pointers.
Approach 1: split() and parseInt() (Recommended)
Idea: split both strings on dots and compare the parts as integers, using 0 for missing parts.
- Split each version with
split("\\."); the dot must be escaped because it is a regex. - Loop up to the longer number of parts.
- Parse each part, or use 0 if it is missing.
- Return at the first difference; otherwise return 0.
Time: O(n + m) · Space: O(n + m).
Approach 2: Two-Pointer Parser
Idea: read one number at a time from each string by accumulating digits until a dot or the end.
- While either string has characters left, read the next number from each (0 if exhausted).
- Compare the two numbers and return at the first difference.
- Skip the dots and continue.
Time: O(n + m) · Space: O(1).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: split() and parseInt() (Recommended)
Integer.parseInt("02") returns 2, so leading zeros are handled for free.
public class VersionSplit {
// Compares part by part as integers; a missing part counts as 0
static int compare(String a, String b) {
String[] x = a.split("\\.");
String[] y = b.split("\\.");
for (int i = 0; i < Math.max(x.length, y.length); i++) {
int p = i < x.length ? Integer.parseInt(x[i]) : 0;
int q = i < y.length ? Integer.parseInt(y[i]) : 0;
if (p != q) {
return p < q ? -1 : 1;
}
}
return 0;
}
public static void main(String[] args) {
String[][] cases = {{"2.5", "2.10"}, {"1.0.0", "1"}, {"3.0.1", "3.0"}, {"1.02", "1.2"}};
for (String[] c : cases) {
System.out.println("\"" + c[0] + "\" vs \"" + c[1] + "\" -> " + compare(c[0], c[1]));
}
}
}
"2.5" vs "2.10" -> -1 "1.0.0" vs "1" -> 0 "3.0.1" vs "3.0" -> 1 "1.02" vs "1.2" -> 0
Time complexity: O(n + m) · Space complexity: O(n + m).
Approach 2: Two-Pointer Parser
No arrays or substrings, which matters when versions are compared in a hot loop.
public class VersionPointers {
// Reads one number at a time from each string, without split() or parseInt()
static int compare(String a, String b) {
int i = 0;
int j = 0;
while (i < a.length() || j < b.length()) {
int p = 0;
while (i < a.length() && a.charAt(i) != '.') {
p = p * 10 + (a.charAt(i++) - '0');
}
int q = 0;
while (j < b.length() && b.charAt(j) != '.') {
q = q * 10 + (b.charAt(j++) - '0');
}
if (p != q) {
return p < q ? -1 : 1;
}
i++; // skip the dots
j++;
}
return 0;
}
public static void main(String[] args) {
String[][] cases = {{"2.5", "2.10"}, {"1.0.0", "1"}, {"3.0.1", "3.0"}, {"1.02", "1.2"}};
for (String[] c : cases) {
System.out.println("\"" + c[0] + "\" vs \"" + c[1] + "\" -> " + compare(c[0], c[1]));
}
}
}
"2.5" vs "2.10" -> -1 "1.0.0" vs "1" -> 0 "3.0.1" vs "3.0" -> 1 "1.02" vs "1.2" -> 0
Time complexity: O(n + m) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| split() and parseInt() | O(n + m) | O(n + m) |
| Two-Pointer Parser | O(n + m) | O(1) |
Here n and m are the lengths of the two strings. Each character is read once; the split version also stores the parts.
Interview Tips and Recommended Approach
- Escape the dot.
split(".")treats the dot as "any character" and returns an empty array. - Never compare versions as strings.
"2.10".compareTo("2.5")says 2.10 is older, which is wrong. - Pad missing parts with zeros. It makes
1,1.0and1.0.0equal. - Ask about suffixes. Labels such as
-betaor-SNAPSHOTneed extra rules.
Recommended Approach: split() and parseInt()
Why: it is short, clearly correct and uses standard library calls that handle leading zeros automatically.