Given two strings containing digits and dot, compare them in the form of a version number.
Intuition
Since the leading zeros are required to be ignored, we use dot character as separator and compute the integer of each part and compare them individually.
Approach
We use two index i
and j
to iterate over s1
and s2
, we move the index i
to the next dot character and compute the integer num1
we have found. Same operations for j
to obtain the integer num2
. Then num1
and num2
are compared.
Complexity
Time complexity: iterate each string once. $$O(n)$$
Space complexity: no extra space needed. $$O(1)$$
Code
|
|