Simple c++ solution if u r struck


#1

int Solution::compareVersion(string a, string b) {
int i=0,j=0,m=a.length(),n=b.length();
unsigned long long int val1=0,val2=0;
while(i<m or j<n){
val1=0,val2=0;
while(i<a.length() and a[i]!=’.’){
val1=(val110)+(a[i++]-‘0’);
}
while(j<b.length() and b[j]!=’.’){
val2=(val2
10)+(b[j++]-‘0’);
}
if(j<n and b[j]==’.’) ++j;
if(i<m and a[i]==’.’) ++i;
if(val1>val2) return 1;
else if(val1<val2) return -1;
}
return 0;
}


#2

Language: C++, Complexity: O(length of bigger string)

Pure String solution

string getnext(string &A, int &i)
{
    while(i<A.size() && A[i] == '0')
        i++;

    string res;
    while(i < A.size())
    {
        if(A[i] == '.')
            break;
        
        res = res + A[i];
        i++;
    }
    i++;
    return res;
}
int compare(string a, string b)
{
    if(a == b)
        return 0;
    if(a.size() == b.size())
        return (a<b)? -1: 1;
    
    return (a.size()<b.size())? -1: 1;
}
int Solution::compareVersion(string A, string B) 
{
    if(A==B)
        return 0;
    for(int i=0, j=0, c; i<A.size() || j<B.size(); )
    {
        c = compare(getnext(A, i), getnext(B, j));
        
        if(c!=0)
            return c;
    }
    return 0;
}

#3

Thanks…Lovely. It was really a good help indeed.