C++ easy soln by grouping numbers in between two dots


#1

int Solution::compareVersion(string a, string b) {

vector<string>A,B;
string temp="";
for(int i=0;i<a.size();i++)
{
    if(a[i]=='.'){
        int k=0;
      
        while(temp[k]=='0')k++;
      
      A.push_back(temp.substr(k,temp.size()-k));
        temp="";
        
    }
    else 
    {
        temp+=a[i];
    }
    
}
if(temp.size()!=0){
    int k=0;
    
        while(temp[k]=='0')k++;
      
        A.push_back(temp.substr(k,temp.size()-k));
}
temp="";
 for(int i=0;i<b.size();i++)
{
    if(b[i]=='.'){
        int k=0;
        
        while(temp[k]=='0')k++;
        
        B.push_back(temp.substr(k,temp.size()-k));
        temp="";
        
    }
    else 
    {
        temp+=b[i];
    }
    
}
if(temp.size()!=0){
    int k=0;
  
        while(temp[k]=='0')k++;
        
        B.push_back(temp.substr(k,temp.size()-k));
}

// for(int i=0;i<A.size();i++)cout<<A[i]<<" “;cout<<endl;
//for(int i=0;i<B.size();i++)cout<<B[i]<<” ";cout<<endl;

while(A[A.size()-1].size()==0)A.pop_back();
while(B[B.size()-1].size()==0)B.pop_back();


int i=0,j=0;
while(i<A.size()&&j<B.size())
{
    string x=A[i],y=B[j];
    if(x.size()>y.size())return 1;
    else if(x.size()<y.size())return -1;
    else
    {
        int u=0,v=0;
        while(u<x.size()&&v<y.size())
        {
            if(x[u]<y[v])return -1;
            else if(x[u]>y[v])return 1;
            else {u++;v++;}
        }
        i++;j++;
    }
    
}
if(i!=A.size())return 1;
else if(j!=B.size())return -1;
else
{
    return 0;
}

}