I solve this by splitting both version strings into lists of integers, padding the shorter one with zeros, and then comparing element by element.
class Solution:
def compareVersion(self, version1: str, version2: str) -> int:
v1 = list(map(int, version1.split('.')))
v2 = list(map(int, version2.split('.')))
length = max(len(v1), len(v2))
v1.extend([0] * (length - len(v1)))
v2.extend([0] * (length - len(v2)))
for a, b in zip(v1, v2):
if a < b:
return -1
elif a > b:
return 1
return 0- Time: O(n)
- Space: O(n)