Last updated on January 5th, 2025 at 01:20 am
Here, we see a Compare Version Numbers LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.
List of all LeetCode Solution
Topics
String
Companies
Apple, Microsoft
Level of Question
Medium
Compare Version Numbers LeetCode Solution
Table of Contents
1. Problem Statement
Given two version numbers, version1
and version2
, compare them.
Version numbers consist of one or more revisions joined by a dot '.'
. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character. Revisions are 0-indexed from left to right, with the leftmost revision being revision 0, the next revision being revision 1, and so on. For example 2.5.33
and 0.1
are valid version numbers.
To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1
and 001
are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0
. For example, version 1.0
is less than version 1.1
because their revision 0s are the same, but their revision 1s are 0
and 1
respectively, and 0 < 1
.
Return the following:
- If
version1 < version2
, return-1
. - If
version1 > version2
, return1
. - Otherwise, return
0
.
Example 1:
Input: version1 = “1.01”, version2 = “1.001”
Output: 0
Explanation: Ignoring leading zeroes, both “01” and “001” represent the same integer “1”.
Example 2:
Input: version1 = “1.0”, version2 = “1.0.0”
Output: 0
Explanation: version1 does not specify revision 2, which means it is treated as “0”.
Example 3:
Input: version1 = “0.1”, version2 = “1.1”
Output: -1
Explanation: version1’s revision 0 is “0”, while version2’s revision 0 is “1”. 0 < 1, so version1 < version2.
2. Coding Pattern Used in Solution
The coding pattern used in all the provided implementations is “String Parsing and Comparison”. This pattern involves breaking down strings into smaller components (e.g., splitting by a delimiter or iterating character by character), converting them into comparable formats (e.g., integers), and then performing comparisons.
3. Code Implementation in Different Languages
3.1 Compare Version Numbers C++
class Solution { public: int compareVersion(string version1, string version2) { int ans = 0; int n1 = version1.size(), n2 = version2.size(); for(int i=0,j=0; i<n1 || j<n2; ++i, ++j) { string s1 = "", s2 = ""; while(i<n1 && version1[i] != '.') { if(s1.size() == 0 && version1[i] == '0') { ++i; continue; } s1 += version1[i++]; } while(j<n2 && version2[j] != '.') { if(s2.size() == 0 && version2[j] == '0') { ++j; continue; } s2 += version2[j++]; } if(s1.size() < s2.size()) return -1; else if(s2.size() < s1.size()) return 1; ans = s1.compare(s2); if(ans < 0) return -1; else if(ans > 0) return 1; } return 0; } };
3.2 Compare Version Numbers Java
class Solution { public int compareVersion(String version1, String version2) { int temp1 = 0,temp2 = 0; int len1 = version1.length(),len2 = version2.length(); int i = 0,j = 0; while(i<len1 || j<len2) { temp1 = 0; temp2 = 0; while(i<len1 && version1.charAt(i) != '.') { temp1 = temp1*10 + version1.charAt(i++)-'0'; } while(j<len2 && version2.charAt(j) != '.') { temp2 = temp2*10 + version2.charAt(j++)-'0'; } if(temp1>temp2) return 1; else if(temp1<temp2) return -1; else { i++; j++; } } return 0; } }
3.3 Compare Version Numbers JavaScript
var compareVersion = function(version1, version2) { var v1Array = version1.split('.'); var v2Array = version2.split('.'); var length = Math.max(v1Array.length, v2Array.length); for (var i = 0; i < length; i++) { var num1 = parseInt(v1Array[i]) || 0; var num2 = parseInt(v2Array[i]) || 0; if (num1 == num2) { continue; } return num1 > num2 ? 1 : -1; } return 0; };
3.4 Compare Version Numbers Python
class Solution(object): def compareVersion(self, version1, version2): main1, _, rest1 = ('0' + version1).partition('.') main2, _, rest2 = ('0' + version2).partition('.') return cmp(int(main1), int(main2)) or \ len(rest1+rest2) and self.compareVersion(rest1, rest2)
4. Time and Space Complexity
Time Complexity | Space Complexity | |
C++ | O(max(n, m)) | O(max(n, m)) |
Java | O(max(n, m)) | O(1) |
JavaScript | O(max(n, m)) | O(max(n, m)) |
Python | O(max(n, m)) | O(max(n, m)) |
- C++ and JavaScript use string splitting and substring storage, leading to higher space complexity.
- Java uses integer variables for comparison, making it more space-efficient.
- Python uses recursion, which can lead to higher space usage due to the call stack.