This problem asks use to compare two version strings and return an answer accordingly. These versions are inspired by software versions where a version string could be “1.0”, “1.0.1”, “2.3.4” and so on.
The problem also hints at comparing from left to right, this means we have to compare each character in both strings from left to right and arrive at the answer.
Return -1 if version1 < version2
and 1 if version1 > version2
The way I have solved below takes heavy inspiration from 21. Merge Two Sorted Lists. We start comparing from the left and go to the smallest length. Then, if we haven’t found a concrete conclusion (-1 or 1
), our current answer is still undecided (0
). So we continue searching if our residual remaining list (a.k.a the longer version string) has any non-zero integers, this means this longer string is a higher version. If there are all zeros, that means both versions are equal.
Code:
Python3
Big O Analysis
-
Runtime
The runtime complexity here is . This is an example of a linear 1-dimensional DP, where we only iterate the input once. Had it been a nested iteration it could have been .
-
Memory
The memory usage is , since we require a dictionary to store already calculated results.
— A