# Python3 solution with worst case O(M) where M is smallest list in case of '.' (dots)

#1

Code looks lengthy but I think it is efficient. Best cases are in O(1) time. Worst case it will loo through the smallest length of list which will be created because of the split operation. There is scope of improvement by reducing few splits. Kept debug info commented.

``````def _check(self, no1, no2):
#print("input--{}--{}--".format(no1,no2))
if int(no1) > int(no2):
return 1
elif int(no1) < int(no2):
return -1
else:
return 0
``````

def compareVersion(self, A, B):

``````    if '.' not in A and '.' not in B:
#print("if-1")
return self._check(A,B)
elif '.' not in A and '.' in B:
#print("if-2")
check_op = self._check(A,B.split('.')[0])
minor_v = int(B.split('.')[1])
if check_op==0:
if minor_v==0:
return check_op
elif minor_v>0:
return -1
else:
return check_op
elif '.' in A and '.' not in B:
#print("if-3")
check_op = self._check(A.split('.')[0],B)
#print("--{}--".format(check_op))
minor_v = int(A.split('.')[1])
if check_op==0:
if minor_v==0:
return check_op
elif minor_v>0:
return 1
else:
return check_op

v1=A.split('.')
v2=B.split('.')
l1=len(v1)
l2=len(v2)
runner=0

if l1-l2>0:
loop=l1
else:
loop=l2

for runner in range(loop):
#print("Loop-{}, len1-{},len2-{}, {}--{}".format(runner,l1,l2,v1[runner],v2[runner]))

check_op = self._check(v1[runner],v2[runner])
if check_op==1 or check_op==-1:
return check_op

if (runner+1)>=l1:
return -1
if (runner+1)>=l2:
return 1

return 0``````