mardi 17 septembre 2019

Compare version number strrings

"The following is the link to the problem I'm trying.

https://www.interviewbit.com/problems/compare-version-numbers/

I simulated the array to compare both the versions. But I couldn't find any mistake in the code."

 int compareVersion(string A, string B) {

// vnum stores each numeric part of version

      long long int vnum1 = 0, vnum2 = 0; 

// loop untill both string are processed

    //  storing numeric part of version 1 in vnum1 
    int i=0,j=0;
    while(i<A.length()||j<B.length()){


    while (i < A.length()&&A[i]!='.') 
    {    
        vnum1 = vnum1 * 10 + (A[i]-'0'); 
        i++; 
    } 

    //  storing numeric part of version 2 in vnum2 
    while (j < B.length()&&B[j]!='.') 
    { 
        vnum2 = vnum2 * 10 + (B[j] - '0'); 
        j++; 
    } 

    if (vnum1 > vnum2) 
        return 1; 
   else  if (vnum2 > vnum1) 
        return -1; 

    //  if equal, reset variables and go for next numeric 
    // pari++
    vnum1=0;
    vnum2=0;
    i++;
    j++;

   }  return 0; 
   }

Input:A="444444444444444444444444" B="4444444444444444444444444" Actual: 1 Expected: -1

Aucun commentaire:

Enregistrer un commentaire